I am trying to build a simple wordpress plugin and I encountered an issue with add_action function.
In my main php, I did
function menu_register(){
.
.
.
add_submenu_page( self::$menu_slug, self::$test_gp_title, self::$test_gp_page_title, self::$capabilities, self::$test_gp_slug, array(__CLASS__, "menu_gp_db"));
}
and
function menu_gp_db(){
include_once(plugin_dir_path( __FILE__ )."ui/drag_and_drop.php");
}
In my drag_and_drop.php, I realized add_action functions do not work at all.
echo "TEST";
class myTest {
function __construct() {
echo "what is going on?";
}
}
function tree_survey_init2(){
global $myTest;
$myTest = new myTest();
}
function myTest(){
echo "ABC";
}
add_action("init","tree_survey_init2");
add_action("myTest","myTest");
echo "END";
The result is
TEST
END
Can I get some advice?
You are trying to add the class, functions and hooks inside add_sumenu_page
callback function:
function menu_gp_db(){
echo "TEST";
class myTest { /**/ }
function tree_survey_init2(){ /**/ }
function myTest(){ /**/ }
add_action("init","tree_survey_init2");
add_action("myTest","myTest");
echo "END";
}
When it should be only:
function menu_gp_db(){
echo "TEST";
echo "END";
}
I think you can merge the rest of code back inside main.php
.