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?