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

Bluehost.com Web Hosting $3.95

Reading Time: 7 minutes

It’s been a while since I last graced you with a tutorial. That said, you guessed right if you guessed today’s post is a tutorial. 🙂

To be exact, this automation tutorial discusses in great detail how to programmatically create WordPress posts and pages using PHP.

I operate a number of WordPress websites that I spend a good bit of time, like 5 to 6 hours daily of repetitive tasks, manually curating and updating content.  There has to be a better way right?

Unfortunately, it only took 6 months for me to question and realize that I could automate everything right down to scheduling each WordPress post using PHP.

It wasn’t that I didn’t know, but it was more like would I stop and set aside the time to invest in planning and executing a sound automation strategy.  Good ole’ procrastination…

Nevertheless, imagine how freeing and liberating it is to programmatically automate the aforementioned repetitive tasks.

Now, instead of 5-6 hours of daily manual work, I simply upload the my files to a web server and use an automated daily cron job to create and schedule WordPress posts.

I’ve gone from hours down to exactly 3 minutes tops of manual labor, and you can do the same too when completing today’s tutorial.

Tutorial Pre-Requisites

Before starting, be sure to have the following pre-requisites met:

  • WAMPP, XAMPP, or LAMP Web or local server
  • MySQL version(s) 5 or greater
  • PHP version(s) 5.3 or greater
  • WordPress version(s) 4.7 or greater
  • Text editor of your choice – Notepad++, Sublime, Dreamweaver or TextWrangler

In addition, be sure to know which timezone your web or local server is set too. You may have to set it via the php.ini, httpd.conf, or another system file containing timezone settings.

The timezone of your web or local server will greatly impact when a post is scheduled and published.

Also, be sure you know how to access the MySQL database via phpMyAdmin, or command line interface or console.

Feel free to also use Lynda.com to research videos and tutorials for any of the listed pre-requisites.

Accessing built-in WordPress functions

Within the root directory of your WordPress website directory, create a new folder. The folder I created for this tutorial is the test folder.

Within the text folder, create and save a file named test.php using the text editor of your choice.

You could create the test.php file as a file that lives in the WordPress root directory, but I personally like keeping scripting files in their own PHP folders.

The first step in the test.php file is to require the wp-load.php file which loads and offers access to built-in WordPress functions. Specifically, we’ll use the WordPress function wp_insert_post() to create today’s post.

Defining and setting post variables

Next, set the post variables to be passed as an array object to the wp_insert_post() function.

There are quite a few post variable parameters that wp_insert_post() accepts, but for the sake of this tutorial, use the following:

  • postType – set this variable to ‘post’ or ‘page’
  • userID – you’ll likely need to access MySQL table for WordPress users to obtain the distinct id of the user you would like the post or page to be published by.
  • categoryID – set a single category id or a chain of integer ids separated by commas (e.g. ‘2,3,4,5’)
  • leadTitle – set this to the desired title
  • leadContent – set this to be the HTML string for your post’s content
  • review how-to tutorial set a featured image

Defining, setting and calculating time variables

The next phase of this tutorial can be a bit tricky to understand when you’re attempting to create only post.

The easy thing to do would be to set the timeStamp variable and not have any calculations whatsoever.

However, I wanted to share with you how you might add a bit more functionality to the script with a bit more elbow grease.

Essentially, you could run this scripting for multiple tasks using a built-in PHP foreach statement that iterates each task.

Doing this allows you to set the iCounter variable to increment by one via each task iteration. Then you could simply set the time increments (i.e., minuteIncrement) multiplied by the iCounter variable for each posting to not post at the same time but to have staggered posting times.

The tricky part of all of this is knowing what timezone your web or local server is set too, and how many minutes (i.e., adjustClockMinutes) should be added for each posting to be posted at the desired time slot.

For the sake of this tutorial, we’re simply scheduling the post to be published one minute into the future via the timeStamp variable’s WordPress friendly formatted date.

Create and bind variables, and make insert

We’re almost home free. The final step is to bind all necessary variables (see and read more on wp_insert_post() arguments and default values) into an array (i.e., new_post).

Once the new_post variable array has been created, it can now be passed to the wp_insert_post() function as an argument.

The final step is creating a post_id variable and set it equal to wp_insert_post(). Upon successful entry of a page or post in WordPress, the wp_insert_post() function returns an id.

Last but not least, we use a simple if else statement to determine whether wp_insert_post() function return an id. If successful, then post insertion executed. If not, then post insertion execution failed.

We then echo the finaltext variable to the web browser display.  That’s it.

Bring it together and test your code

It’s time to bring each section of code together, save it and test out your technical prowess.

Now that you can programmatically create WordPress posts and pages using this PHP script, think of all the many things you could do.

Although I didn’t cover it specifically, but if you know how to parse files via PHP, then you could create any one of the following:

  • A deals website
  • A content website
  • A jobs website
  • and many other affiliate marketing ideas…

If you’re new to affiliate marketing or could simply use some ideas for how to automate affiliate marketing, then I encourage you to read and STUDY the following books:

Good luck with automating your future, realizing time savings, and let me know if you have any questions whatsoever.

Watch video for more in-depth details

And for those of you that love watching video tutorials, I created a video too. Enjoy! 😉

Please subscribe to my YouTube channel to receive tutorial alerts.

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 digital strategies for greater commerce.

20 Responses to Programmatically create WordPress Posts and Pages using PHP

  • TJ says:

    Hey this is a very helpful post! Do you know how to add tags to the post programmatically? Either way, thanks for this. It will save me alot of time.

  • Mike says:

    Hi Alvin,

    Very informative but I have one question. This only runs when you hit the file correct? How would I schedule this so it happens every 15min?

    • Alvin Brown says:

      Hi Mike – Correct, the file executes only when loaded in the web browser. If you’re in search of automating the execution of the file “every x min”, then you’ll likely need to investigate cron jobs. What type of environment is your php file hosted in (i.e., Linux, Windows, etc.)?

  • Tim Chen says:

    Hi Alvin,

    Thanks for your post
    this post helped me a lot!

    currently i can successfully post a post,
    but now wondering how can i add a “featured image” to an auto posted post,
    do you know which function or where i should look at?

    the webpage i entered was the page i apply auto update function… 🙂

    Thanks!

  • Daniela Lopes says:

    Hi Alvin,
    Thanks a lot for posting this. The only problem I have is I don´t host the WordPress files in my computer. How can I manually add the new php code in the WordPress dashboard?

    Thank you 🙂

  • Zavrazhnov Kirill says:

    Hi! Thank you for your great tutorial! That is awesome. I have a question. I have a json file which contains multiple data for the posts. So, right I can add the first array of data using json_decode. But i need to create more posts using json. Thank you!

    • Alvin Brown says:

      WordPress treats iframe as malicious code, sanitizes it. You’ll have to use a plugin design to include iframes.

  • Great Tutorial I created the bulk posts in WordPress using this tutorial but I have a doubt.

    Is creating posts is programmatically is similar to creating the post manually, is there is any effect on SEO. I mean after creating post the WordPress system ping the post to pinging services like that .

    My clear doubt is, “Is programmatically creating WordPress posts effects the SEO ?”

    • Alvin Brown says:

      Hi Bala! Thanks and I’m glad that you found the tutorial to be helpful. As for “effect on SEO”, that’s really a loaded question. A greater, more in-depth conversation would need to be had to discover whether or not programmatically creating WordPress posts effects SEO. There are so many different factors to consider when attempting to realize the value of such a statement. As for content ranking, there really is no substitution for human-written content — even AI and Machine Learning have not gotten to the level to “fool” search engines, although well on their way. That being said, if you do automate content creation using WordPress, then I recommend that content created or curated be presented in a unique manner that doesn’t suggest duplicate content. Doing so, this will allow for the content to reap the greatest opportunity for search result ranking for given content topic. Hope this helps. Feel free to respond and share a bit more detail about your question.

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.