Insert & Update WordPress Blog Posts With This PHP Script

In my last post I mentioned our new promotion system that can “duplicate” a previous promotion by updating the dates, creating a new coupon code or scheduling items to go on sale, and then creating a new newsletter, blog post, landing page, etc. One of the big challenges for me was coming up with the correct way to insert and update new blog posts in WordPress so that we never actually have to log in to WordPress to manage our promotions. We’re able to do everything from one centralized system.

It took a bit of research and some trial & error, but I eventually came up with a relatively simple solution that uses the wp_insert_post function. It’s important to do this as opposed to inserting/updating the database directly because when WordPress updates there are often changes to the database or other functionality, but this function should be relatively future proof.

This script can be used as a starting point if you’re trying to accomplish something similar. Hopefully it will save you a bit of time. Being able to create and update WordPress posts from another CMS or from an automated system opens up all sorts of interesting opportunities!

<?php 

define('WP_USE_THEMES', false); //we don't want to output our theme
require('../wp-blog-header.php'); //the wp-blog-header.php file is in your main WordPress directory. I placed this script in it's own directory and password protected it, hence the ../

$post_id = 0; //0 inserts new post, greater than 0 updates existing post with that post_id	
$post_author = 1; //1 = default user
$post_category = array(1); //1 = default category			
$post_title = 'My First Auto Post';
$post_content = 'Testing an auto post! Look how easy this is';
$post_date = '2017-01-01 00:00:00';	//post will be scheduled for this date

//insert into WordPress
$my_post = array(
  'ID' => $post_id,
  'post_date' => $post_date,
  'post_title'    => $post_title,
  'post_content'  => $post_content,
  'post_status'   => 'future',
  'post_author'   => $post_author,
  'post_category' => $post_category
);
	
$post_id = wp_insert_post( $my_post );

if ((is_int($post_id)) && ($post_id > 0)) {

	$permalink = get_permalink($post_id, true);
	$guid= get_the_guid($post_id);
	$post_info = get_post($post_id);
	if ($post_info->post_parent > 0) {
		$post_info_parent = get_post($post_info->post_parent);
		$post_name = $post_info_parent->post_name;
	} else {
		$post_name = $post_info->post_name;
	}
	$output = array(
		'status' => 'success',
		'post_id' => $post_id,
		'permalink' => $permalink,
		'guiid' => $guid,
		'post_name' => $post_name,
		'post_parent' => $post_info->post_parent
	);

} else {
	$output = array(
		'status' => 'fail',
		'status_info' => $post_id
	);	
}

//output json
header('Content-Type: application/json');	
echo json_encode($output);

?>

No comments yet. Be the first!

Leave a Reply

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

Commenting Rules

I'm honored that you found this post interesting enough to leave a comment. Before posting, I have a few ground rules:

  • Please keep your comments as relevant to the post as possible.
  • No personal attacks or any other nastiness.
  • Your first comment is subject to my approval.

Thanks!