Automattic / WP-Cron-Control

This plugin allows you to take control over the execution of cron jobs.
20 stars 7 forks source link

Manual cron run #1

Open szepeviktor opened 9 years ago

szepeviktor commented 9 years ago

@ethitter What do you think about a button that loads /wp-cron.php manually, let's say by an AJAX request?

szepeviktor commented 9 years ago

Instead of clicking on all Run Now-s.

szepeviktor commented 9 years ago
<?php
/*
Plugin name: Manual cron
Plugin URI: https://github.com/szepeviktor/manual-cron
Description: Triggers wp-cron by an AJAX call and displays possible output.
Version: 1.0.0
License: The MIT License (MIT)
Author: Viktor Szépe
GitHub Plugin URI: https://github.com/szepeviktor/manual-cron
*/

add_action( 'wp_dashboard_setup', 'o1_manual_cron_add_widget' );

function o1_manual_cron_add_widget() {

    wp_add_dashboard_widget(
                 'manual_cron_widget',
                 'Manual WP-cron',
                 'o1_manual_cron_output'
    );
}

function o1_manual_cron_output() {

    $trigger_script = '
<script>
(function ($) {
    var $output = $("#manual-cron-output");

    $("#manual-cron").click(function () {
        $.ajax({
            url: "%s",
            method: "POST",
            timeout: %d,
            dataType: "html",
            success: function (data) {
                $output.html("<code>OK</code>" + data);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                $output.html("<code>ERROR</code> " + textStatus + " " + errorThrown);
            }
         });
     });
}(jQuery));
</script>
    ';

    $doing_wp_cron = sprintf( '%.22F', microtime( true ) );
    $url = add_query_arg( 'doing_wp_cron', $doing_wp_cron, site_url( 'wp-cron.php' ) );
    $timeout = apply_filters( 'manual-cron-timeout', 20000 );

    print '<button id="manual-cron" class="button button-primary">Run now</button>';
    print '<div id="manual-cron-output" style="margin-top: 8px;"></div>';
    printf( $trigger_script, $url, $timeout );
}
szepeviktor commented 8 years ago

@ethitter Could you include this code in the plugin?

szepeviktor commented 8 years ago

https://github.com/szepeviktor/manual-cron