1

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();
        }

migrated from webapps.stackexchange.com May 31 '11 at 20:56

This question came from our site for power users of web applications.

2

You should be including this file:

    include( "/some/path/wordpress/wp-config.php" );

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.

  • 2
    No, please include wp-load.php instead of the config file. The wp-config file can live in different places, wp-load can't. – Otto Jun 28 '11 at 16:57
1

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.

1

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/

0

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.

0

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy