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.
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.
August 25, 2009
That’s the nice thing about WP… it very simple and user-friendly.
-Jack