Wordpress

Running WordPress Admin Functions from a Cron Job

March 26, 2009   ·   By   ·   3 Comments   ·   Posted in Wordpress, Writing Plugins

The quick version?  Include wp-load.php and wp-admin/admin-functions.php in a file in the plugin directory, and point your cron there.  For those of you with nothing important to do, a long winded explanation:

I ran into a project a few days ago where the client needed a simple plugin put together – read an xml file, pull some information from it, and turn it into a post.  Initially, the client wanted to just write straight to the database, but considering we’ve got access to a number of functions perfectly suited for the job, I knew things would be much easier if we turned it into a simple plugin, and ran the built in functions from the admin side.

The plugin came together quite nicely – RSS read, categories created, post inserted, all like a charm.  It occurred to me at that point that the client wanted this to run automatically at set intervals – he wanted to schedule a cron job to handle it.  However, since the functions I needed to run exist on the admin side, I assumed I’d have to jump through some serious hoops to get a cron to run it.

I formulated a plan, and was all ready to implement it – the plan was to access wp-admin/options.php with the proper get parameters to run the function.  It turns out, using curl, you can set a cookie, and then use it to access another page – very cool (big thanks to this post from Russell Heimlich for that;  I’m sure more thorough documentation is out there, but his is the post I found).

I still wasnt crazy about the idea though – the extra input with the cron meant more work for the client, more possibility for things to go wrong, and more time spent on support for me.  So I decided to see if I could just include the right files from outside the admin section, and poof – it worked.  Here’s how it works:

You’ve got to include 2 files to get access to admin side functions:  First, wp-load.php.  wp-load.php gets everything set up, and fires up wordpress.  However, we’re calling this function from the plugin folder, inside the content directory (as opposed to the admin directory) – so when wp-load is called, we’re not going to be in the admin section, and we’re not going to get access to those functions.  On the bright side, we also don’t have to deal with WordPress forcing you to login.  Since we still need those admin functions, include wp-admin/admin-functions.php.  This loads up the admin side and gives us access to the admin functions – and we’re set to go.

So that’s it.  Include those  2 files, call wp_insert_post() (or whatever you need to call), and make the client happy.

Off topic – if you’re tired of expensive web hosts, I’ve got a friend who is starting to host for people inexpensively: check out his post on cheap web hosting here.

3 Comments
  1. That’s the nice thing about WP… it very simple and user-friendly.
    -Jack

  2. Dan

    Hi there Peter, mate would you have any idea how to use/implement the ‘includes’ what you mentioned above,

    Im struggling with the Postie plugin. Postie doesn’t work properly unless you run the get-mail.php file logged in as admin. There for im trying to login, get admin priveledges and then execute the script.

    Any advice would be fantastic.

    • Well Dan, if you’re working with a plugin that is checking user level, I’d guess that you’re going to need to login, receive the cookie set by the login process, and then make that cookie available when you make the request to do whatever it is you’re trying to do. I haven’t had a need to try it, so I’m just shooting from teh hip here, but maybe these links will help:

      First, you’ll need to send a curl request to yoursite.com/wp-login.php with authentication information included in a post request. The two fields you’ll need to POST are “log”, containing the username, and “pwd” containing the password.

      At that point, you’ll need to retreive the cookies set by wordpress, and store them somewhere. After that, it should be as easy as forming another curl request to get-mail.php, and providing the cookies that were just set. Having them available should give you admin priveleges, and allow you to run the script.

      This page has a good example of how to send a curl request, retrieve the cookies, and then use them in a later request.

      Again – I’m just guessing here, as I’ve never used the postie plugin, but that ought to at least get you pointed in the right direction.

      Good luck!

Submit a Comment