WordPress.org

Ready to get started?Download WordPress

Forums

wp_schedule_event - Creating PHP script (18 posts)

  1. WTD
    Member
    Posted 7 years ago #

    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.

  2. WTD
    Member
    Posted 7 years ago #

    anyone help? :'(

  3. Joe Hall
    Member
    Posted 7 years ago #

    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.

  4. WTD
    Member
    Posted 7 years ago #

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

  5. Joe Hall
    Member
    Posted 7 years ago #

    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.

  6. WTD
    Member
    Posted 7 years ago #

    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

  7. Joe Hall
    Member
    Posted 7 years ago #

    "I can also set them all to a future date,"

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

  8. WTD
    Member
    Posted 7 years ago #

    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

  9. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    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.

  10. WTD
    Member
    Posted 7 years ago #

    thanks mate, will try that out!

    Rgds,
    Steve

  11. WTD
    Member
    Posted 7 years ago #

    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

  12. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

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

  13. WTD
    Member
    Posted 7 years ago #

    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??

  14. WTD
    Member
    Posted 7 years ago #

    anyone help? still stuck with this one :(

  15. WTD
    Member
    Posted 7 years ago #

    still no joy :'(

  16. Asido
    Member
    Posted 7 years ago #

    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!

  17. madteckhead
    Member
    Posted 7 years ago #

    Hi,
    Interesting thread. I am looking to bulk import data from a local db (via RSS or something) and format it as posts to save in the wordpress database. Has anyone got ideas on how to write a script (pref. using wordpress functions) to add posts? I'd like it to be made a plugin, and for it to periodically check the remote RSS feed.

    Any ideas appreciated.

    Nathan

  18. WTD
    Member
    Posted 7 years ago #

    thanks asido, i had seen that before and checked it when writing my import script.

    I was using a standard hook publish_future_post as my callback, so bit bummed that it doesn't work :( oh well i'm gonna keep on plugging away at it!

Topic Closed

This topic has been closed to new replies.

About this Topic