Multisite run wp cron on each site

How do you run wp cron on all your multisite blogs? I'd like to have the server crontab do this.

  • Timothy
    • Chief Pigeon

    Some other handy stuff:

    https://premium.wpmudev.org/blog/schedule-functions-in-your-plugins-with-wordpress-cron/

    If you want to disable WP Cron and have your server do it, then you can do that with this in your wp-config.php file:

    define('DISABLE_WP_CRON', true);

    That disables WP Cron.

    If in cPanel you can schedule each blog and use this:

    wget -q -O - http://yourwebsite.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1

    If you are creating the crontab in say SSH and you wish to run it every hour of every day then you would add this instead:

    5 * * * * wget -q -O - http://yourwebsite.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1

    If your really stuck then it might be wise asking your host for a hand with this.

    Cheers.

  • taropaa
    • The Incredible Code Injector

    @Timothy, doing what you suggested works for a single blog. But on multisite that will only trigger cron for that one domain.

    Here is how you would get cron to run properly on all sites:

    Creata a file in / called wp-cron-multisite.php:

    <?php
    require('./wp-load.php');
    global $wpdb;
    $sql = $wpdb->prepare("SELECT domain, path FROM $wpdb->blogs LIMIT 0,300", '');
    
    $blogs = $wpdb->get_results($sql);
    
    foreach($blogs as $blog) {
        $command = "http://" . $blog->domain . ($blog->path ? $blog->path : '/') . 'wp-cron.php?doing_wp_cron';
        $ch = curl_init($command);
        $rc = curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
        $rc = curl_exec($ch);
        curl_close($ch);
    }
    ?>
    

    Then enter this command in SSH

    */5 * * * * wget -q -O - "http://mydomain.com/wp-cron-multisite.php?doing_wp_cron=date +\%s" > /dev/null 2>&1

    You would be scheduling the cron file you created to run every 5 minutes.

    And add the
    'define('DISABLE_WP_CRON', true);'

    Source:
    http://www.lucasrolff.com/wordpress/why-wp-cron-sucks/

  • Timothy
    • Chief Pigeon

    Hey there,

    Thanks for sharing and marking me unhelpful. Sure if it's a site like WP.com where new sites are created all the time then my method isn't best.

    But if it's your own network the cron can be set per site if needed as explained by Mika:

    http://halfelf.org/2013/wp-cron-it-up/

    You may wish to use the code tags next time in your posts. It will render the code better and ensure none is removed or missed. (as seen in screenshot)

    Thanks again for sharing I'm sure it will help many! :slight_smile:

  • Tang Rufus
    • Design Lord, Child of Thor

    Would it be better if I use

    foreach($blogs as $blog) {
        $command = "http://" . $blog->domain . ($blog->path ? $blog->path : '/') . 'wp-cron.php?doing_wp_cron';
        wp_remote_get( $command );
    }

    instead of

    foreach($blogs as $blog) {
        $command = "http://" . $blog->domain . ($blog->path ? $blog->path : '/') . 'wp-cron.php?doing_wp_cron';
        $ch = curl_init($command);
        $rc = curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
        $rc = curl_exec($ch);
        curl_close($ch);
    }
  • Timothy
    • Chief Pigeon

    You would be using a native WP function to run the file :slight_smile:, I've not tested, did you have any issues?

    If you need any further support then it might be best if you open your own, even if the issues seem closely related. That way it will remain independent of this one and ensure it doesn't confuse issues here if this thread needs reopening again by the author.

    Posting on old threads can also lead to us missing your questions.

    It also means the member who started this thread or anyone following it isn't inundated with post notifications each time we respond to each other. A lengthy thread could annoy the original thread starter

    This also allows us to better track and answer your questions to ensure we don't miss any for ya.

    If the current thread is relevant then please feel free to link back to this in your new one.

    You can start a new thread here:

    https://premium.wpmudev.org/forums/#question

    Thanks.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.