Adding feature image to a post [Wordpress coding]

Discussion in 'WordPress' started by Googl, Mar 3, 2011.

  1. #1
    Hi all,

    I am not so sure this is the right place to post this, but I have seen similar threads here... This is not through admin but via scripting/coding.

    I am creating a plugin that will create posts from external data. I have managed to insert posts and post meta, etc, but I haven't yet found out how to add a feature image to the post. I know how the images are saved through the wordpress insert functions but setting posts feature images is what I don't know how.

    Does anyone know the functions for this task? I can't find it in Wordpress reference.

    Thanks.
     
    Googl, Mar 3, 2011 IP
  2. Googl

    Googl Active Member

    Messages:
    509
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    75
    #2
    Could sometime give me some tip on how to do this. A link to where I can find some help as well will be useful.
     
    Googl, Mar 4, 2011 IP
  3. skabt

    skabt Banned

    Messages:
    285
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    123
    #3
    i insert photo under custom field, not sure it work for u or not ...
     
    skabt, Mar 4, 2011 IP
  4. Googl

    Googl Active Member

    Messages:
    509
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    75
    #4
    What's custom field? What do you mean exactly?
     
    Googl, Mar 4, 2011 IP
  5. Googl

    Googl Active Member

    Messages:
    509
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    75
    #5
    Okay, I have figured how to do this. It is actually very simple but you have to consider other factors. So this won't be a matter of calling the function and it work. All you do is call;

    set_post_thumbnail( $postid, $post_images_id );

    But you have to make sure that the file that has that function is included somewhere. The function looks like this;

    function set_post_thumbnail( $post, $thumbnail_id ) {
    	$post = get_post( $post );
    	$thumbnail_id = absint( $thumbnail_id );
    	if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) {
    		$thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'thumbnail' );
    		if ( ! empty( $thumbnail_html ) ) {
    			update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id );
    			return true;
    		}
    	}
    	return false;
    }
    PHP:
    I thought I post it here just in case someone will need it some day.


    Another question if someone would help me. I know when you make a new post wordpress activates all the installed plugins. When I make a new post the plugins do not take effect. For example the wp-to-twitter plugin won't apply. Is there a way to make all the plugins to be active?
     
    Googl, Mar 5, 2011 IP
  6. otjutt

    otjutt Member

    Messages:
    166
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    33
    #6
    I also faced the same problem few months back but I left the project. If anyone know how to do this do share with us.
     
    otjutt, Mar 5, 2011 IP