0

I created a plugin for specific heavy batch operation but it seems that every time I click on any other place (list posts , list page , ....) the heavy operation is launched again, so I want to know how i can launch the plugin operation only when I activate him like singleton.

bellow the code I use to avoid this kind of behavior but it doesn't work:

register_activation_hook( __FILE__, 'my_plugin_activation' );
function my_plugin_activation() {
  add_option( 'my_plugin_activation','just-activated' );
}

add_action( 'admin_init','my_plugin_initialize' );
function my_plugin_initialize() {
    if( is_admin() && get_option( 'my_plugin_activation' ) == 'just-activated' ) {
    delete_option( 'my_plugin_activation' );
        flush_rewrite_rules();
    }
}

Could you help .

  • What does your heavy operation do? Keep in mind that things you do on one page request have no effect on another, each page request is built from scratch. PHP isn't like Node or Python apps where a single program handles all requests, when you request a page in PHP everything is loaded in from scratch, nothing persists unless you store it in the database or filesystem. Have you considered a custom WP CLI command? – Tom J Nowell Feb 19 at 15:18
  • admin_init runs on every admin page. If you're wanting to only run the batch job when the plugin is activated, move the operation inside of your my_plugin_activation() function. If instead you're wanting it to run periodically, you could set up a cron job, or you could set it up only to run when a specific admin screen is visited. – WebElaine Feb 19 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.