[link_popup id='2715' link_text='Click to open popup' name='Main Pop Up']
Websites generating greater customer growth and revenue.

30%* off! Big Savings For Your Small Business!

Reading Time: 6 minutes

A few weeks ago, I received an email and comment from a reader in search of an easy method to programmatically create WordPress posts and pages using php.

The reader was successfully able to create a WordPress post and page using the tutorial, but shed light on another challenge when creating an automated post:

How does one set a featured image for a programmatically created post?

In this tutorial, I won’t dive deep into the details of the inner workings of creating a post. Again, there’s a previous tutorial for that. 😉

To level set, this tutorial will assist you in programmatically setting a featured image for a WordPress Post or Page using PHP, and based on the following:

  • Not using native WordPress Set Feature for Post or Page Image functionality and user interface
  • No form submission or upload functionality
  • No post or page creation

This tutorial strictly covers the following using PHP programming:

  • uploading of a file to the WordPress Media Library with necessary attachment attributes and metadata
  • associating newly uploaded image to post as a featured image

Tutorial Pre-Requisites

Before you eagerly dive into the tutorial and its code, be sure you have the following pre-requisites met as identified in the previous tutorial.

Without having met pre-requisites, today’s tutorial WILL NOT OPERATE. Nevertheless, let’s get started with today’s tutorial.

Accessing Built-In WordPress Functions

For this tutorial, I’ve created a directory named upd within the root directory of my WordPress environment, and have created a file named test.php.

One of the first things needed for this tutorial is the ability to access WordPress functions. To do so, you’ll need to require wp-load.php — loads and offers access to built-in WordPress functions — in test.php.

Defining Featured Image Variables and Image Existence

Next, define and set the following variables to be used when uploading image to WordPress Media Library and associating the image file to a WordPress post as a featured image:

  • postID – set this value equal to a Post or Page id (integer)
  • IMGFileName – set this value to be the entire file name, including extension
  • dirPath – leave set to PHP’s getcwd method, which returns the current file or directory path
  • IMGFilePath – set to be concatenation of dirPath and IMGFileName variables — full path of image that is to be uploaded to WordPress Media Library
  • message – this is the text message to be displayed should IMGFileName variable value not exist in directory

Once the aforementioned variables are successfully defined with their respective values, it’s time to check and validate the specified image file to be set as featured image exists in the same directory as test.php file.

To do so, use PHP’s file_exist method via a if statement, passing to it the IMGFileName variable as an argument.

If the image exists, then additional code will be executed and the appropriate value for the message variable is set within this code.

Also, don’t forget to echo the message variable. It’s used to communicate human readable progress of script execution to the web browser.

Uploading and Associating Image to WordPress Post or Page as Featured Image

Okay! Now its time to get down to the nitty gritty details you’ve been waiting — how to programmatically set featured image for WordPress Post or Page using PHP.

This next 60 or so lines of code needed to achieve this tutorial’s objective will occur within the if statement of the previous section.

Within this same section, the following built-in WordPress functions are used to set feature image to a given WordPress Post or Page:

Reviewing the list of functions above, one would assume there would be a wp_featured_image function of some sort. But no, it’s really the set_post_thumbnail that is the wp_featured_image equivalent.

Technically speaking, set_post_thumbnail is really all you need if the image already exists in your WordPress Media Library.

But remember this tutorial is about how to programmatically upload image to WordPress Media Library.

With a bit more elbow grease, you could create your own upload form using PHP and then replace IMGFileName variable’s value with the respective file POST and FILE variables. 😉

For the sake of time, I won’t explain the code line by line, but do read the comments in association to the function definitions above to get the gist of what’s going on throughout the code (see below).

Last but not least, we need to know whether the code executed or not. Defining success variable to be equal to set_post_thumbnail, create an if statement for the success variable.

If the desired WordPress Post or Page has a featured image successfully set, then set the message variable to indicate such messaging. Otherwise, state feature image has not been set.

And believe it or not, this is all that’s needed to you need to programmatically set a featured image for a WordPress Post or Page using PHP. Any questions or comments?

Video: How To Programmatically Set A Featured Image

And for those of you that love video tutorials, watch and learn in this quick video how to programmatically set a featured image for a WordPress Post or Page using PHP. Enjoy! 😉

Entire Tutorial Codebase

Copy, paste, and modify the code below, and you’re ready to test away.  Good luck!

Meet Alvin Brown

He's an experienced and passionate serial entrepreneur, founder and publisher of Kickstart Commerce. Alvin possesses a great love for startups dominating their market using profitable internet marketing and domain name strategies for greater commerce.

2 Responses to Programmatically Set Featured Image for WordPress Posts and Pages using PHP

Leave a Reply

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

*



Browse by topic

THE DAILY SCOOP

Subscribe today and receive instant access to need-to-know Domain marketing strategies, auctions, promo codes and other FREE resources.

Enter your email, and you're on your way!

Unsubscribe at any time.