0
votes

I'm writing my first Wordpress Plugin and having trouble with the register_activation_hook.... What I want is to have a variable declared in the register_activation_hook and echo it on one of the menu pages.

I've broken out my code into separate file and have included a brief version below of the 2 files below:

define('TEST_PLUGIN', rtrim( plugin_dir_path( __FILE__ ), '/' ));    

function test_plugin_install() {        
    $test="TEST";
}

register_activation_hook( __FILE__, 'test_plugin_install' );

add_action( 'admin_menu', 'test_plugin_add_menu' );

function test_plugin_add_menu() {
    add_menu_page( 'Test Plugin Page', 'Test Plugin', 'manage_options', 'test_plugin', 'test_plugin_settings' );
}

function test_plugin_settings() {
    require( TEST_PLUGIN_DIR . '/test_plugin_settings.php' );       
}

And in test_plugin_settings.php I use:

global $test;
echo $test;

I know that the test_plugin_settings.php works as my code has tabs and other content that get rendered in the menu page. However the $test variable does not.

Also in the function test_plugin_install() I also create sql tables... which are created so I know that works. Interestingly, the sql only executes if the plugin is being installed for the first time, ie the plugin directory is deletes and sql tables cleared.

This makes me think that register_activation_hook is only called for the first time and not when the plugin is just deactivated and then reactivated.

Is this correct?

1
what you exactly need you just need to clear your concept or you need to flush table on deactivation of plugin ? - A Shah
I just want the variable $test to be echo'd onto a menu page. I've tried putting global $test onto every php I've used but still no luck - Caveman
global $test; $test = "hi"; now use $test to print anywhere in you plugin files - A Shah
I've tried that, but still no luck unfortunately, it's like the register_activation_hook doesn't fire - Caveman

1 Answers

0
votes

register_activation_hook will be triggered only on activation, and used to install some options or create table, while admin_menuneeds to be triggered on admin_init (action to init something for wp-admin).