Weekly Post Generator


Image from page 191 of “Hardware merchandising August-October 1912” (1912) flickr photo by Internet Archive Book Images shared with no copyright restriction (Flickr Commons)

Given there’s been some recent interest in using WordPress as destination for content created on other services, I thought it might be helpful to see how I set up my weekly Pinboard posts. I did this a year or two ago so it’s sloppy but I think it’ll show some stuff that matters — mainly loading up the array variables for wp_insert_post.

This is a super-simple example that inserts one post based on the content at the curl URL and is run weekly via a cron job. Looking at it now, I am full of shame as it didn’t need an external page etc. etc. but in the interest of actually writing the post and being transparent on how you don’t have to be competent to get stuff done I present it anyway.

function make_post (){  
//gets the content for the body of the post
    $curl = curl_init();
    curl_setopt ($curl, CURLOPT_URL, "******LOCATION OF CONTENT YOU WANT********"); //in my case I did a little pinboard API integration to generate this content- if it were JSON you'd have to parse it etc. 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec ($curl);
    curl_close ($curl);

//create the weekly portion for the title - ie 2017-06-25
    $date = date_create('now');
    $lastweek = date_sub($date, date_interval_create_from_date_string('7 days')); 
    $lastweek = date_format($date, 'Y-m-d');

//pushes the content into the various WordPress boxes
    $my_post = array(
    'post_title'    => 'Weekly Web Harvest for ' . $lastweek ,
    'post_content'  => $result,
    'post_status'   => 'publish',
    'post_author'   => 1,
    'post_category' => array( 601 )
  );
 
// Inserts the post into the database
   wp_insert_post( $my_post );

}

You can do similar things via javascript and you can loop it against a json feed with multiple elements.

It gets a little confusing in my head when I start to think how you’d deal with syncing changes long-term. For instance, if you have a post that was created via this API cycle and it gets updated a month later how would you know? I guess you can write something to check back individually every X amount of time but that seems like it’d get messy/bad for performance if you had hundreds or thousands of posts.

Comments on this post

  1. aarondavis1 said on July 5, 2017 at 7:05 pm

    Thank you Tom. This spurs my thinking once again, especially in regards to scripts, automation and triggers.

Leave a Reply

Trackbacks and Pingbacks on this post

No trackbacks.

TrackBack URL