简体   繁体   中英

How to run filter hook inside the action hook for crob job event in WordPress? Any idea

I am working on cron job event to enable and disable wordpress automatic updates. I prepare cron job event and call this filter action auto_update_plugin but i couldn't properly called in functions.php. Cron job event succesfullly works but filter does not add_filter( 'auto_update_plugin' , '__return_true' , 10 , 2 ) . If I use this filter out side the hook it works but inside the hook it does not work. ab_automatic_update_cron_true is a cron event which I am running. any help would be appreciated what I am wrong.

I want to enable and disable wordpress automatic updates. Do you have any suggestion or code please share it.

add_action( 'ab_automatic_update_cron_true', 'cw_true_function' , 10 , 2);

function cw_true_function() {         
    return add_filter( 'auto_update_plugin' , '__return_true' , 10 , 2 );   
}

Usually when something like this happen it may be because the add_action comes too late and the action has already been executed. I don't know if your cron event actually works or not, that's a whole other problem but for your code you could try to edit it like this:

if (!did_action('ab_automatic_update_cron_true')){
    add_action( 'ab_automatic_update_cron_true', 'cw_true_function' , 10 , 2);
}else{
    cw_true_function();
}

function cw_true_function() {
    return add_filter( 'auto_update_plugin' , '__return_true' , 10 , 2 );
}

This way you are telling wordpress to execute the function if the action has already been executed. Maybe this can point you in the right direction

NOTE: You are telling that your function 'cw_true_function' should accept 2 params (see the 4th param of add action) but you are actually expecting none, is that ok?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM