add action no response

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?

Solutions

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.

Tags: PHP / Wordpress

Similar questions

AJAX response in WordPress administration empty but the browser response is not empty
I've added a custom page in wp-admin. The page, among other things, has this script: The response of the script, when viewed in the Network tab of the browser's inspector is not empty - depending on what happens in the background (server), its contents may vary, but it's always a JSON, and never empty. However, the console.log(data) bit shows up as...
Wordpress ajax-action failing because of newline in response
When I'm using the dashboard, for example, adding a category. The request returns valid XML as expected, but it is prepended with a newline character, causing the DOMParser() to fail. I'm not sure where to proceed for debugging, it happens on one site and not another (different server, different theme). I've checked, none of the theme's php files h...
ajax action after ajax loaded response in wordpress
i use ajax in wordpress to display content of a post by clicking in a list of posts. my ajax : my function : and how i declare ajax in wordpress : my problem is : when the content is displaying with ajax, i want to add next / prev button to change displaying content but i got no action, no response, the click doesn't work like if it was no jquery s...
Wordpress Frontend plugin error with ajax calling in response from action function
In the main plugin file, i wrote this code. And in script file code is: There is no error in my code when i check it through search console. But "schemarating" div is updated to "0". means no response_from_the_action_function is received. I am not able to locate the error. Help me out please.
ajax response is working without wp_ajax action
HTML: Here is my simple jquery code to append a row of a form: Ajax Action : And show_slider_form_input(); definition is as follows which is inside absb_bt_to_s-admin-display.php: Everything is cool and working as expected. But if I remove following code: It works as usual. As far I know the above code is only responsible to push response. I search...
Ninja forms - Using custom action, how can I handle the form's status based on the api's response?
Using a custom action, I post the submissions of a form to an third-party API route. This part works well, i.e. the data is received and properly processed by the api (I've created a custom plugin to handle all of the api's requests). Basically, on submit, the custom action is triggered and its callback fires the api request. What I can't figure ou...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.