• Hello

    First of all: I'm a newbie. This is the first time I code something in PHP. So I need a plugin to automaticly delete posts after 24 hours. Because I couldn't find a plugin with the featrue I need, I decided to code my one one. So I tried something.

    In this code I set the time, when a post should deleted. (Maybe it’s wrong)

    $timestamp = time(); //Gets the seconds since 1.1.1970
    $time = date("d.m.Y-H:i", $timestamp); //Formats the seconds in the actual time
    $calculateTime = strtotime($time) + 24*3600;  // adds 24h to the timestamp (24*3600=24h)
    $FormatedTime = date("d.m.Y-H:i", $calculateTime); //Formats the calculated date
    
    $deleteTime = $FormatedTime; //Sets a better understandabel value.

    Next I implemented this:

    wp_schedule_event($deleteTime, $daily, do_action('delete_post', 2488));

    2488 Is the post ID that I test with. I understand that daly at the $deleteTime the post should be deleted, but it doesn’t work.

    My second question is:

    I’d like that every post in a defined category will be deleted after 24h. So I write something like this. (But it is’t true i guess…)

    if (in_category(43))
    {
    
    //code here
    }

    Thanks a lot for every help!

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    wp_schelule_event() only accepts date/time as a UNIX timestamp. Formatted date strings are invalid. For a timestamp some amount of time from now, add that interval in seconds to the number returned by time(). To get a timestamp for a particular date, use mktime().

    Keep in mind you should never have a need to call do_action() unless you anticipate other developers wanting to customize your code. If you are calling it, you are likely doing it wrong.

    Scheduling an event involves 4 steps.
    1. Register your activation callback. Pass the name of your callback function that schedules the event.

    2. Declare your activation callback function. This is where you call wp_schedule_event(). Besides the timestamp and interval, pass an action tag. You cannot pass values in scheduled events, the values must be stored through other means: globals, transients, class properties, cookies, etc. Ensure your tag does not conflict with possible core actions and filters. ‘delete_post’ is a bad tag name, there is an existing core action by that name. ‘jonas_delete_post’ is a much better tag name. (I prepend all my function and tag names with ‘bcw_’ to avoid name collisions)

    3. Add a callback function to the action tag you created in step 2. Use add_action() to do this, not do_action().

    4. Declare the callback function you added in step 3. This is the function that does the scheduled task. Everything else is just setting up a schedule. Here you call delete_post(). For now you can hardcode the post ID, but it’ll need to come from one of the vehicles mentioned in step 2 eventually.

    Finally, scheduled events will not happen unless the site is visited after the scheduled time. This means events will not happen precisely on time except for very busy sites. If you schedule events on a test server or one very infrequently visited, you’ll need to visit the site yourself to trigger the events. Requesting any page will do, home page, login page, single post, whatever.

Viewing 1 replies (of 1 total)
  • The topic ‘wp_schedule_event and delete post’ is closed to new replies.