admin管理员组

文章数量:1316832

My plugin is running on every single WordPress page, and I can't find a WordPress hook so that the script will only run when the plugin is activated. I only want part of the script to run when the plugin is activated, but not when I navigate to other WordPress pages. Apologies if it's a simple or basic solution I'm quite new to this.

I've tried register_activation_hook(__FILE__, 'do_my_function' ) but that gives me an error:

the plugin generated 1500 characters of unexpected output during activation. If you notice headers already sent messages...

If I try to debug this, all I get is Fatal Errors and a whole lotta blank space even though I'm not even outputting anything.

Here's the code:

class plugin
{
    
    public static function activate()
    {   
        flush_rewrite_rules();
        //(new self) -> do_my_function(); //if I use this line instead of add_action it causes the "plugin generated 1500 characters of unexpected output" error
    }
    
    function do_my_function(){}

    function deactivate()
    {
        flush_rewrite_rules();
    }
}

if( class_exists( 'plugin' ) )
{
    $plugin = new plugin();
}
register_activation_hook( __FILE__, array($plugin, 'activate'));    
register_deactivation_hook( __FILE__, array($plugin, 'deactivate'));

add_action('admin_init', array($plugin, 'plugin_activated'), 10, 0); //causes plugin to run on every wordpress page load

本文标签: hooksMy plugin runs on every single WordPress pagebut I want it to run only on activation