ArticleTrader.com
  

 Main Menu

  Home
  Member Login
  Forum
  Submit Article
  Membership
  RSS Feeds
  Contact Us
  About

 Services

  Article Distribution
  Link Building

 Tools

  ArticleMS
  Directory Tracker

 Categories

  Automotive
  Business
  Computers
  Entertainment
  Finance
  Food
  Health
  Home and Family
  Internet
  » Affiliate Programs
  » Blogging
  » Domains
  » Email
  » Forums
  » Online Business
  » PPC Advertising
  » RSS
  » Security
  » SEO
  » Site Promotion
  » Spam
  » Web Design
  » Web Hosting
  Legal
  Science
  Self Improvement
  Shopping
  Society
  Sports
  Technology
  Travel
  Writing

187 users online.



 
  » Category Sponsors
  Get Your Link Here - Limited Time Bargain at only $11/month!

Home » Internet » Web-design » Custom Field Image to Featured Image In Wordpress

spinxwebdesign
Article written by spinxwebdesign

View Full Profile
Get Html Code
PDF | Print View | Post to your Site

Custom Field Image to Featured Image In Wordpress

Submitted by spinxwebdesign
Tue, 17 Jan 2012

Are you using theme of wordpress which having a proper and professional web design according to your business? Do you have a theme that uses custom fields to hold image URLS and wish you could automatically set these as the featured post image, to be more attuned with other themes if you ever decided to change? Then friends you are lucky due to the code present at "free-php.net". So, here below is the code for you:

Note: "$" is used to declare variable
"preg_match" returns the number of times pattern matches. That will be either no match which is 0 or 1 time.

// First lets get the custom field , we are assuming its called "thumb"
$thumb = get_post_meta($post->ID, 'thumb', true);

// Check if a post thumbnail is already set, if so don't do anything.
if( has_post_thumbnail( $post->ID ) ) {

} else {

if ( ! empty($thumb) ) {

// Download file to temp location
$tmp = download_url( $thumb );

// Set variables for storage
// fix file filename for query strings
preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $thumb, $matches);
$file_array['name'] = basename($matches[0]);
$file_array['tmp_name'] = $tmp;

// If error storing temporarily, unlink
if ( is_wp_error( $tmp ) ) {
@unlink($file_array['tmp_name']);
$file_array['tmp_name'] = '';
}

// do the validation and storage stuff
$id = media_handle_sideload( $file_array, $post->ID, NULL );
// If error storing permanently, unlink
if ( is_wp_error($id) ) {
@unlink($file_array['tmp_name']);
return $id;
}

// Set the featured image
set_post_thumbnail( $post->ID, $id );

}

}

 

Spinx - Leading Los Angeles Web Developers Company offers specialized Web Application Development expert services in all programming language.


Source: ArticleTrader.com
Creative Commons License

Comments

No comments posted.

Add Comment

You do not have permission to comment. If you log in, you may be able to comment.

 Top Authors

 1 Stebee (3270)
 2 limalan88 (2920)
 3 alien82 (2756)
 4 kajuba (2508)
 5 sverdlow (1712)
 6 jamiehanson (1705)
 7 juliet (1691)
 8 MarkeD (1296)
 9 robertoms2003 (1296)
 10 AnthonyF (1244)
 11 articles (1205)
 12 artavia.seo (1148)
 13 spinxwebdesign (1119)
 14 gprather (1071)
 15 LouieLiu (1069)

 Distribution

Article Distribution

  
  Affiliate Program 2Checkout.com, Inc. is an authorized retailer of ArticleTrader.com

0.01s