Wordpress cronjob every 3 minutes

天大地大妈咪最大 提交于 2021-02-06 08:41:47

问题


there are many post on stackoverflow with this topic but (i dont know why) nothing will work for me.

What i have

function isa_add_every_three_minutes( $schedules ) {

    $schedules['every_three_minutes'] = array(
            'interval'  => 180,
            'display'   => __( 'Every 3 Minutes', 'textdomain' )
    );

    return $schedules;
}
add_filter( 'cron_schedules', 'isa_add_every_three_minutes' );


function every_three_minutes_event_func() 
{
    // do something
}

wp_schedule_event( time(), 'every_three_minutes', 'every_three_minutes_event_func' );

Any ideas what i'm doing wrong?


回答1:


You need to use add_action() to hook your function to the scheduled event.

add_action( 'isa_add_every_three_minutes', 'every_three_minutes_event_func' );

Here is the full code.

// Add a new interval of 180 seconds
// See http://codex.wordpress.org/Plugin_API/Filter_Reference/cron_schedules
add_filter( 'cron_schedules', 'isa_add_every_three_minutes' );
function isa_add_every_three_minutes( $schedules ) {
    $schedules['every_three_minutes'] = array(
            'interval'  => 180,
            'display'   => __( 'Every 3 Minutes', 'textdomain' )
    );
    return $schedules;
}

// Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'isa_add_every_three_minutes' ) ) {
    wp_schedule_event( time(), 'every_three_minutes', 'isa_add_every_three_minutes' );
}

// Hook into that action that'll fire every three minutes
add_action( 'isa_add_every_three_minutes', 'every_three_minutes_event_func' );
function every_three_minutes_event_func() {
    // do something
}
?>



回答2:


seems you forgot to use the command add_action()

See this exemple: http://wpguru.co.uk/2014/01/how-to-create-a-cron-job-in-wordpress-teach-your-plugin-to-do-something-automatically/




回答3:


First of its important where this code is in, is it in your functions.php for your theme? Or is it a custom plugin your developing?

Through my experience its both easier to debug and activate cron through a custom plugin, using activation hooks to activate and deactivate events. I have had hard times activating cron events through functions php before, I prefer activating these events through custom plugins.

I would start with a plugin structure like this:

  • /my-cron-plugin
  • /my-cron-plugin/index.php
  • /my-cron-plugin/my-cron-plugin.php

index.php contents:

<?php // silence is golden

my-cron-plugin.php contents:

<?php

/**
 * Plugin name: My Custom Cron Plugin
 * Description: Simple WP cron plugin boilerplate.
 * Author: Your name
 * Version: 0.1
 */

// Security reasons...
if( !function_exists( 'add_action' ) ){
    die('...');
}

// The activation hook
function isa_activation(){
    if( !wp_next_scheduled( 'isa_add_every_three_minutes_event' ) ){
        wp_schedule_event( time(), 'every_three_minutes', 'isa_add_every_three_minutes_event' );
    }
}

register_activation_hook(   __FILE__, 'isa_activation' );

// The deactivation hook
function isa_deactivation(){
    if( wp_next_scheduled( 'isa_add_every_three_minutes_event' ) ){
        wp_clear_scheduled_hook( 'isa_add_every_three_minutes_event' );
    }
}

register_deactivation_hook( __FILE__, 'isa_deactivation' );


// The schedule filter hook
function isa_add_every_three_minutes( $schedules ) {
    $schedules['every_three_minutes'] = array(
            'interval'  => 180,
            'display'   => __( 'Every 3 Minutes', 'textdomain' )
    );
    return $schedules;
}

add_filter( 'cron_schedules', 'isa_add_every_three_minutes' );


// The WP Cron event callback function
function isa_every_three_minutes_event_func() {
    // do something
}

add_action( 'isa_add_every_three_minutes_event', 'isa_every_three_minutes_event_func' );

After having setup this plugin, the event should be activated upon plugin activation. To test if its working use this plugin: https://wordpress.org/plugins/wp-crontrol/

One other good resource to understand how WP cron work is: https://developer.wordpress.org/plugins/cron/



来源:https://stackoverflow.com/questions/39442982/wordpress-cronjob-every-3-minutes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!