Quantcast
Channel: Toolset » All Posts
Viewing all articles
Browse latest Browse all 20145

Reply To: [Waiting for user confirmation] Featured Image from image field

$
0
0

Well,

I got it going. But it does take a bit of extra code from a second function. I'm wondering if you can see a way to reduce the overhead on this or program it better:


// This code makes a "Featured Image" thumbnail from an image custom field
// Must be put into your themes functions.php file
// Must be used with FJarrets Function which can be found at:
// http://frankiejarrett.com/get-an-attachment-id-by-url-in-wordpress 
// ***** Change "wpcf-whoswho-main-image" to the name of the field you wish to pull from ******

function field_to_featured() {
	// Skip everything if thumbnail already exists
	$already_has_thumb = has_post_thumbnail($post->ID);
	if (!$already_has_thumb)  {
		global $post;
		//Get URL of image from the Custom Field
		$whos_imageurl=get_metadata($meta_type="post", $item_id=$post->ID, $meta_key="wpcf-whoswho-main-image", $single=true);
			// Skip if Custom Field is empty
			if ($whos_imageurl) {
				$attachment_id = fjarrett_get_attachment_id_by_url( $whos_imageurl ); //Use FJarrets Function to convert to an image ID number
				set_post_thumbnail($post->ID, $attachment_id); //Actually write the new ID number into the thumbnail field
				}
	}
}  //end function
// These add actions based on when you update a post. Works with Custom Post Types too. Any can be removed.
add_action('save_post', 'field_to_featured');
add_action('draft_to_publish', 'field_to_featured');
add_action('new_to_publish', 'field_to_featured');
add_action('pending_to_publish', 'field_to_featured');
add_action('future_to_publish', 'field_to_featured');
add_action('the_post', 'field_to_featured'); //This one is really agressive and adds a thumbnail just by looking at a post!

Describing what this does:

1 –
It checks If there is already a thumbnail. That way this can be used with other thumbnail plugins and not fight each other. Once one is there, this won't activate.

2 -
We need to access our Custom Field and extract out the ID number of the image – The image must be uploaded to our media library. This assumes you're using the 'image' field and a user (Front/back end) has uploaded a picture to this field in a normal way.

A) The field contains a URL to the image, so we get this.
B) We check if the field is empty. Why else continue?
C) We convert the URL into an ID that can be placed into the thumbnail field. This is where we need Fjarrets code to do that conversion for us.

3-
We actual set it.

4-
At the end are the hooks to activate the function throughout the system.

NOTE:
If anyone can find a cleaner/faster way to just get the ID, then we wouldn't have to go through hoops to get to it. VERY OPEN TO IDEAS!

Here is the other function your need to place in your functions.php:

/* Get an attachment ID by URL in WordPress
 * http://frankiejarrett.com/get-an-attachment-id-by-url-in-wordpress
 * Return an ID of an attachment by searching the database with the file URL.
 *
 * First checks to see if the $url is pointing to a file that exists in
 * the wp-content directory. If so, then we search the database for a
 * partial match consisting of the remaining path AFTER the wp-content
 * directory. Finally, if a match is found the attachment ID will be
 * returned.
 *
 * @return {int} $attachment
 */
function fjarrett_get_attachment_id_by_url( $url ) {
 
	// Split the $url into two parts with the wp-content directory as the separator.
	$parse_url  = explode( parse_url( WP_CONTENT_URL, PHP_URL_PATH ), $url );
 
	// Get the host of the current site and the host of the $url, ignoring www.
	$this_host = str_ireplace( 'www.', '', parse_url( home_url(), PHP_URL_HOST ) );
	$file_host = str_ireplace( 'www.', '', parse_url( $url, PHP_URL_HOST ) );
 
	// Return nothing if there aren't any $url parts or if the current host and $url host do not match.
	if ( ! isset( $parse_url[1] ) || empty( $parse_url[1] ) || ( $this_host != $file_host ) ) {
		return;
	}
 
	// Now we're going to quickly search the DB for any attachment GUID with a partial path match.
	// Example: /uploads/2013/05/test-image.jpg
	global $wpdb;
 
	$attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->prefix}posts WHERE guid RLIKE %s;", $parse_url[1] ) );
 
	// Returns null if no attachment is found.
	return $attachment[0];
}

Viewing all articles
Browse latest Browse all 20145

Trending Articles