Insert WordPress page via external (cron) script?

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.

Read More

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

Related posts

Leave a Reply

5 comments

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

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