WordPress.org

Ready to get started?Download WordPress

Forums

automatically set the featured image (8 posts)

  1. jojimori
    Member
    Posted 3 years ago #

    Is there a way that the Featured Image of a post can be set automatically without having to select it within the "add post" page?

    so for example in either of the following 2 cases you should be able to see what i mean.
    1. when I create a new post, I insert one image in the post, and then some text, then I publish this. Note, that I haven't set the featured image, but I would like the featured image to default to the single image that has been included in the post.
    2. the second case is that if I wanted to use "post over email", I can send an image for the post no problem, but I would like that image to also be the "Featured Image' of that post, but I don't see any way that this can be done.

    Be great if anybody had any advice on this one!

    Thanks in advance

  2. Purab Kharat
    Member
    Posted 3 years ago #

    I already solved this issue for more information check my article. [Link moderated.]

  3. Rev. Voodoo
    Volunteer Moderator
    Posted 3 years ago #

    @wordpressapi ...
    http://wordpress.org/about/domains/
    just fyi

  4. zonIRC
    Member
    Posted 3 years ago #

    Hi jojimori,

    This really a late reply but since I'm also had encountered such problem(automatically add images as featured images of the post). For a several days I try to get the solution and do some research in internet. Lastly I found the below solution(where the code is from this codex documentation page. I just make it as a function and added the last line for making it as a featured images.

    Hopefully this will help other, remember the $post_id is the parent post ID(where the image been attach) and the $filename is the absolute path of the image files

    function set_featured_image($post_id,$filename) {
    	$wp_filetype = wp_check_filetype(basename($filename), null );
    	$attachment = array(
    		'post_mime_type' => $wp_filetype['type'],
    		'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
    		'post_content' => '',
    		'post_status' => 'inherit'
    	);
    	$attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
    	// you must first include the image.php file
    	// for the function wp_generate_attachment_metadata() to work
    	require_once(ABSPATH . "wp-admin" . '/includes/image.php');
    	$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
    	if (wp_update_attachment_metadata( $attach_id,  $attach_data )) {
    		// set as featured image
    		return update_post_meta($post_id, '_thumbnail_id', $attach_id);
    	}
    }
  5. doctorcilantro
    Member
    Posted 3 years ago #

    @ ZONIRC

    Thanks! Any clue as to where to implement this code?

  6. zonIRC
    Member
    Posted 3 years ago #

    @doctorcilantro

    You can put it into your theme functions.php.

  7. doctorcilantro
    Member
    Posted 3 years ago #

    Possible it needs to go into funcitons.php?

    I tried in my theme functions.php and it didn't work.

  8. zonIRC
    Member
    Posted 3 years ago #

    @doctorcilantro

    Sorry for a late reply, yes it should be ok in functions.php of the theme.

    Another thing, the theme should be enable/support the featured image/post_thumbnail. To do this, you can use something like this: add_theme_support('post-thumbnails');

    To automatically add the 1st image in the post as a featured images, you need to customised it a little bit. Since the code just for assign an image as a featured image/post thumbnail for the post. Probably by hook into post save.

Topic Closed

This topic has been closed to new replies.

About this Topic