• I’m simply trying to use a plugin hook within my own function to add a custom field to each post when it’s published. But I can’t quite figure out how to make the function work. Here’s what I have so far:

    function custom_medium($postID) {
    	global $post;
    
    	$postID = $post->ID;
    
    	$custom = get_the_image( array( 'default_size' => 'large', 'link_to_post' => false, 'image_scan' => true ) );
    
    	update_post_meta($postID, 'medium', ''.$custom.'');
    
    }
    add_action('publish_post', 'custom_medium');

    I’m using the Get The Image plugin to grab the first attached image for each post, and need to register what it finds as a custom field called “medium”.

    As always, any help is greatly appreciated. Thanks in advance.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Use $_POST instead of $post. What to want to do is catch the values being submitted by the form. PHP puts those in the $_POST array. Also, $postID should already be set. You shouldn’t need $postID = $post->ID

    Put the following right at the top of your function to see what you have to work with.

    global $_POST;
    echo $postID,'< br />';
    print_r($_POST);
    exit();

    Obviously, most of that you’ll want to remove later.

    Thread Starter jimmiejo

    (@jimmiejo)

    Thanks for your response apljdi, but I can’t quite get it to work. Here’s what I’m trying based on your suggestion:

    function custom_medium(){
    
    	global $_POST;
    
    	$custom = get_the_image( array( 'default_size' => 'large', 'link_to_post' => false, 'image_scan' => true ) );
    
    	update_post_meta($post_id, 'medium', ''.$custom.'');
    }
    add_action('publish_post', 'custom_medium');

    I also tried modifying the first line to be:

    function custom_medium($post_id){

    …but that didn’t make a difference. While updating and/or publishing a post, the “medium” field is created, but with no value.

    You still need the $postID in your function declaration. WP will populate that for you with the post ID. What you don’t need is this line: $postID = $post->ID. So, first do this.

    function custom_medium($postID) {
    global $_POST;
    echo $postID,'< br />';
    print_r($_POST);
    exit();
    }
    add_action('publish_post', 'custom_medium');

    You should see the post ID and a fairly large array on an otherwise blank page. Its much easier to read if you view the source in your browser. If that works the next issue is going to be with get_the_image(). It looks to me like that function needs to be called from within the loop, and hooking to publish_post is outside the loop.

    The function image_by_scan() can probably be cribbed and altered to do what you want though.

    Thread Starter jimmiejo

    (@jimmiejo)

    Thanks, apljdi. That function does return an array for me. Looking at the Get The Image plugin, I guess the best function to use/modify would be this:

    function image_by_attachment( $args = array() ) {
    
    	extract( $args );
    
    	if ( !$post_id )
    		global $post;
    
    	/* Get attachments */
    	$attachments = get_children( array( 'post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) );
    
    	if ( empty( $attachments ) )
    		return false;
    
    	foreach ( $attachments as $id => $attachment ) :
    		$i++;
    		if ( $i == $order_of_image ) :
    			$image = wp_get_attachment_image_src( $id, $default_size );
    			$image = $image[0];
    			break;
    		endif;
    	endforeach;
    
    	return array( 'image' => $image );
    }

    How would I go about adding that into a function to update_post_meta with publish_post, and work outside the loop?

    Ok. That function basically calls the WordPress function get_children and then sorts the results so you should be able to take the part between /* Get attachments */ and return array( 'image' => $image );, change the $post->ID to $postID, and get the value you want in $image.

    Thread Starter jimmiejo

    (@jimmiejo)

    Tried this to no avail:

    function custom_medium($postID) {
    
    	global $_POST;
    
    	$attachments = get_children( array( 'post_parent' => $postID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) );
    
    	if ( empty( $attachments ) )
    		return false;
    
    	foreach ( $attachments as $id => $attachment ) :
    		$i++;
    		if ( $i == $order_of_image ) :
    			$image = wp_get_attachment_image_src( $id, $default_size );
    			$image = $image[0];
    			break;
    		endif;
    	endforeach;
    
    	return array( 'image' => $image );
    
    	update_post_meta($post_id, 'medium', ''.$image.'');
    }
    add_action('publish_post', 'custom_medium');

    Doesn’t seem to do anything. Going to dig for another function to tap into to grab the first attached image and register it as a custom field, but would still love to hear your suggestions, apljdi.

    Okay, you are returning a value when you hit the line return array( 'image' => $image );. Anything after that line never executes. Just remove that ‘return’ line. You should get something to happen.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Creating a custom field from a plugin hook in functions.php’ is closed to new replies.