How To Set Featured Image Programmatically In WordPress

Are you looking to set featured image through the code in WordPress? Sometimes, we came across a task where we need to write a piece of code which can set post thumbnail for a post, page our custom post types. In this article, we study how to set featured image programmatically in WordPress.

There are a lot of beautiful features provided by WordPress, one of them is Featured Image aka Post Thumbnail. Featured Image is a representative image for post, page or custom post type.

Regular Way To Set Post Thumbnail

You can upload featured image to specific post or page by just going to edit screen and click on ‘Set featured Image’ link. It will open a WordPress media dialog from which we can choose or upload the image.

Set Featured Image

It is a straight-forward way to set post thumbnail in WordPress. By this way, you can set post thumbnail to any post, page or custom post type.

But, as mentioned in the topic, our purpose is to write a code which set post thumbnail without following the above steps.

Set Featured Image Programmatically

For our tutorial, we are creating a form where a user can select an image and this image will set as post thumbnail to the specified post id. For this, you need to place below code in your template file.

<form method="post" enctype="multipart/form-data">
	<div class="form-group">
		<label>Select Image:</label>
		<input type="file" name="image">
	<div class="form-group" style="margin-top:20px;">
		<input type="submit" name="submit" value="Submit">

This code gives you one file input and the submit button.


Now, on the click of submit button, we will write the code as follows.

if ( isset($_POST['submit']) ) {
	$upload = wp_upload_bits($_FILES["image"]["name"], null, file_get_contents($_FILES["image"]["tmp_name"]));
	$post_id = YOUR_POST_ID; //set post id to which you need to set post thumbnail
	$filename = $upload['file'];
	$wp_filetype = wp_check_filetype($filename, null );
	$attachment = array(
		'post_mime_type' => $wp_filetype['type'],
		'post_title' => sanitize_file_name($filename),
		'post_content' => '',
		'post_status' => 'inherit'
	$attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
	require_once(ABSPATH . 'wp-admin/includes/image.php');
	$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
	wp_update_attachment_metadata( $attach_id, $attach_data );
	set_post_thumbnail( $post_id, $attach_id );

In the above code, we used the wp_upload_bits() method which uploads the image and returns absolute and relative path. We have to take a generated absolute path and pass to the next methods like wp_check_filetype(), wp_insert_attachment(), and wp_generate_attachment_metadata().

Now if you go the edit screen of a specific post you will see the post thumbnail under the Featured Image section.

Related Articles

If you liked this article, then please subscribe to our Youtube Channel for video tutorials.

Recommended Tutorials For You

2 thoughts on “How To Set Featured Image Programmatically In WordPress

  1. I am using two websites.One for content writing (source website) and other for publishing (target website).
    I set featured image for one post A on source and write function to publish the post A on target.I fetch post A data from source wp_posts,wp_postmeta and insert the same into destination wp_posts,wp_postmeta.And also call the function set_post_thumbnails() to set featured image.It gets inserted and target website displays the post with feature image on front-end.When I Open target website admin panel and edit the post,I am unable to see the featured image for the same.Instead it shows default image.The image present in the source upload and target uploads folder.When i inspect the path of image,its name changes from image_name.jpg to image_name-696×198.jpg.Any solution or work around for this problem?Thank you in advance.

    1. It should see at the back-end. Not sure what’s going wrong on your site. Maybe some plugins or code are conflicting. I need more details. I can’t give a solution without seeing what plugins or which theme you are using.

Leave a Reply

Your email address will not be published. Required fields are marked *