WordPress.org

Support

Support » How-To and Troubleshooting » wp_schedule_event – Creating PHP script

wp_schedule_event – Creating PHP script

  • Morning/Evening All.

    I have written a small application that automatically inserts a number of posts straight into the wordpress database.

    These are all inserted with an appropriate timestamp, at a given time every day for each of the posts I have inserted.

    Now I understand for these posts to be set from a ‘future’ post to a posted post, then the time passes, I need to set up a entry in the wp_options table and can see the code, wp_schedule_event.

    My problem is, I can’t work out how to use that function.

    For example, I insert 70 posts, each having a time of 9am, and number 1 tommorow, number 2 tommorow, number 3 the next day etc.

    Now these are all in the database, what I’m trying to do now is create a simple PHP script that goes through each of the post ID’s (I have these) and the time that it should be scheduled for (I also have these) but can’t for the life of me can’t get the script I have written to work.

    Attached is a snipped to see if anything is wrong…

    <?PHP
    require_once('admin.php');
    wp_schedule_event(strtotime("Sat Apr 28 12:17:35 2007". " GMT"),'publish_future_post', array(10));
    wp_schedule_event(strtotime("Sat Apr 28 12:18:35 2007". " GMT"),'publish_future_post', array(11));
    ?>

    This is uploaded to the wp_admin area and I run it, but nothing gets inserted.

    My posts are all inserted with with ID’s from 10 to XX

    Any help appreciated.

Viewing 15 replies - 1 through 15 (of 17 total)
  • anyone help? :'(

    I am actually working on the same type of application at the moment. Although going about it differently. I am writing a simple email script that can send a message with my posting in it to WP using the “Post by Email” feature. I am automating the script using a cron job. I like this method because I don’t have to fool around with inserting data to the db. Instead I let WP do that for me. Below is an example of the script I am using:

    ‘<?php
    $to = “SECRETEMAIL@mysite.com”;
    $subject = “POST TITLE”;
    $body = “POST BODY/CONTENT”;

    if (mail($to, $subject, $body)) {
    echo(“<p>Post successfully sent!</p>”);
    } else {
    echo(“<p>Post delivery failed.</p>”);
    }
    ?>’

    The only problem that I am having is setting up my POP3 server to work well with WP. Hope this helps.

    Hmm interesting, how do you specify posts that start in the future, like the date and time etc?

    I don’t. When I want the post to start posting I start the cron job. When I want it to stop posting I just delete the script and then the cron. If you want to automate a time and a date to publish a posting, WP already has a feature for that build in.

    My application is just for those that want a posting to conitually republished over and over again at designated intervals.

    yeah thats the whole point!

    I have 70-150 articles to post, one a day for the next X days, I can insert them into the database fine, as long as I want them to appear all at once. I can also set them all to a future date, but cannot work out how to schedule the CRON job using wp_schedule_event!

    If anyone can help with this, would be much appreciated!

    Thanks,
    Steve

    “I can also set them all to a future date,”

    If you can do this, then why do you need the cron job?

    because just setting them to a future date is not enough to actually register them in the pseudo-cron job.

    when you post (via the new post screen) with a future date entered, and the modify timestamp flag ticked, it enters the date in the posts database (with the future date) and also registers an entry in another table to tell wordpress, that on a given date and time, set this posts status from ‘Future’ to ‘Published’

    Now I can do the first bit OK but can’t get the second (cron bit) working!

    If anyone can take a look at the PHP and see what I am doing wrong it would be appreciated ๐Ÿ™‚

    Thanks,
    Steve

    You should just do it like this. It’s not difficult:

    wp_schedule_single_event($time_goes_here, 'publish_future_post', array($your_post_ID_goes_here));

    Done and done. It will set their post status from future to published at whatever time you want.

    You won’t be requiring admin.php, mind you. Best way to do it is like this:

    <?php
    require('/path/to/your/blog/wp_blog_header.php');
    wp_schedule_single_event($time_goes_here, 'publish_future_post', array($your_post_ID_goes_here));
    ...more...
    ?>

    And then run that manually, just once.

    thanks mate, will try that out!

    Rgds,
    Steve

    Couple of Q’s for you if you have a sec!

    I assume that $time_goes_here would work with the response from strtotime("Tue May 1 02:14:05 2007")

    Also the array($your_post_ID_goes_here) can that be literally

    array(array(10)) if my ID is 10? What else needs to go in that array??

    Cheers

    I assume that $time_goes_here would work with the response from strtotime(“Tue May 1 02:14:05 2007”)

    Sure.

    Also the array($your_post_ID_goes_here) can that be literally array(array(10)) if my ID is 10? What else needs to go in that array??

    No. It would be just array(10). You’re overcomplicating things.

    wp_schedule_single_event(strtotime("Tue May 1 02:14:05 2007 GMT"), 'publish_future_post', array(10));

    Hmm, thanks for the help, but no joy.

    I’ve created the script, checked the dates that I’m entering (all OK) and I print out a message before and after the wp_schedule_event call. (all seems OK)

    but nothing gets entered into the database. It’s as if the call to wp_schedule_event is being ignored.

    Anyway to check by switching on Debug or trace??

    anyone help? still stuck with this one ๐Ÿ™

    still no joy :'(

    Hi mate,

    Check this out:
    http://blog.slaven.net.au/archives/2007/02/01/timing-is-everything-scheduling-in-wordpress/

    By reading the thread so far, I guess the event does not get fired because you did nto turn it into a plugin hook. Check the section titled “Defining the event youโ€™re scheduling”, you will see that there is an example of what needs to be done.

    I really hope this helps your case. Good luck, mate!

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘wp_schedule_event – Creating PHP script’ is closed to new replies.
Skip to toolbar