I’m trying to write a php script that I can run out of cron to create new WordPress pages.
Unfortunately, I can’t find any documentation on how to do this. I’m using the WP scripts rather than directly manipulating the SQL…but still no joy. The below runs with no errors…but also produces no pages. I am sad.
Anyone know how to do this?
#!/usr/local/php5/bin/php
<?php
include ('/some/path/wordpress/wp-admin/admin.php');
include ('/some/path/wordpress/wp-includes/post.php');
$to_insert = array();
$to_insert['post_author'] = 0;
$to_insert['post_content'] = '<h1>Here is my info</h1>n<ul><li>one</li><li>two</li></ul>n';
$to_insert['post_type'] = 'page';
$to_insert['post_status'] = 'publish';
$to_insert['post_title'] = 'We Must Test';
$result = wp_insert_post($to_insert,true);
if ( is_wp_error($result) ) {
echo $result->get_error_message();
}
You should be including this file:
That’ll get you to where you can use the wp_insert_post() function.
If this is a multi-site blog, make sure to use the switch_to_blog() function first.
The files you are trying to include are just small chunks of WordPress core and won’t work without being properly loaded by it. You need to load it properly, see integrating WordPress in Codex.
Your includes are somewhat incorrect, and also you’re missing some other key information that makes up a post (but maybe not a page). On the whole, you’re on the right track.
Check out the script jwz created (and I helped him fix up) to see how to make a post via command line. You can extend the idea to make pages too:
http://www.jwz.org/hacks/wppost.php
http://www.jwz.org/blog/2011/03/wordpress-help/
Maybe I am off, but what are you needing this cron script for? I know you can set a publish date for pages/posts so you can schedule it to post on a certain day/time.
I use a modified version of the txt-as-post plugin. With it you can read in a zip of posts. I then call it via cron (I prefer that way than using WPs built in cron).