William Estep

My journey to retire and sail. Writing, reading, technology, programming, photography, coffee, equality, sailing, and the oxford comma.

PHP Script to Post Post

Category: Programming
Tags: technologywordpressphp

Continuing on with the auto publishing idea from Macdrifter.

I already write all of my posts in Markdown, most include a MultiMarkdown header, and all the post markdown files go in a dropbox folder.

So what do I need:

  • Hazel rule to identify new posts

    • Some indication in the post header that the post is ready to publish
  • Script to convert the post to html

  • Write the new post to my Wordpress blog, setting:

    • Title
    • Category

    • Tags if any

    • Featured Image

What If’s?

  • Should I allow an update to an existing post if the file is updated?

  • What if Category doesn’t exist?

Getting Started

Fletcher Penney has added a built in feature to the new multimarkdown binary that returns file metadata. For example:

    multimarkdown -e title foo.md

Wordpress has an XML-RPC service available, if activated, and php has built in support. So maybe a simple php script will do the heavy lifting. The script is pretty straight forward, but once I started testing, I noticed some encoding issues. For example, with this as my test post, the apostrophe’s were all whack.

Most of the script is copied directly from Macdrifter’s example.

First some variable setup:

So far so good. Now with the file, run it through the multimarkdown commandline foo and return some metadata:

Yes, I’m making several external calls to return the data. But the new multimarkdown is fast, and so far I haven’t noticed any issue. We’ll see how well it does in production, but this wont be a highly taxed script.

The Postlink piece is if I ever decide to go to a Daring Fireball style linked list.

So, at the end of this part, we now have a valid title, category, postlink, and postbody converted to html.

All the echo statements are just for my testing. I’ll probably comment them out once I have stuff working as desired.

So why the coding errors? I’m not sure yet. I’m also not getting the post_id back. I wonder if the problem is the curl method I’m using to post:

Time to rethink. Wordpress uses the Incutio XLM-RPC library. I think I’ll give that a try next. Ok, I grabbed the class-IXR.php file from my wp-includes folder. So, here’s the new function using the new class:

Yeah! This test returned the post_id. That’s a start. And, more importantly, everything is encoded correctly! So far so good.

Note that for testing, I’m having it post with a draft status. That’s the FALSE at the end of the params line. Change that to true, and the post is auto published.

Now, I need to figure out how to set a featured image. Also, now that I have the post id returned, I can add that to the files metadata and if I make any changes, I should be able to script an update to the post instead of a new post. Let’s start with the featured image. According to the WordPress XML-RPC Codex, the featured image, or post thumbnail can be added by post id in the new post content. I have some standard or stock images I like to use as thumbnails, so I should be able to come up with someway to find the post id pretty easily. For testing, I’ll use this image.

I added and checked, but can’t tell if it’s setting the post thumbnail without publishing, so I’ll need to test that a bit later.

Last piece I want to get working is adding the post_id back to the metadata. This is pretty simple thanks to the wonderful array_splice command:

Ok. Script has been updated to handle multiple categories as well. Next post, I’ll start working on the automation piece using Hazel.

Enjoy!

Similar Posts


© William Estep