WP Admin Bar and Ajax Request

I’m trying to get default nodes from the WP Admin Bar through AJAX. I get NULL if I var_dump the variable which should store the Admin bar object.

class my_class
{
var $Admin_bar;
public function __construct(){
$this->PluginSetup();
//Other stuff
}

public function PluginSetup(){
add_action( ‘wp_before_admin_bar_render’, array( $this , ‘admin_bar_load’ ), 1);
add_action( ‘wp_ajax_get_the_page’, array( $this ,’get_the_page’) );
}

public function admin_bar_load() {
global $wp_admin_bar;

//here I set variable
$this->Admin_bar = $wp_admin_bar->get_nodes();

//If I var_dump($this->Admin_bar) I see the object correctly stored
}

public function get_the_page() {

//where I request the page rendering
ob_start();
include ‘inc/mypage.php’;
$response = ob_get_contents();
ob_end_clean();
wp_send_json($response);
}

}

And then in my inc/mypage.php i just:

var_dump($this->Admin_bar);

At this point I get NULL. I’m not sure if my problem is about the variable initialisation or about the actions.

Read more here:: WP Admin Bar and Ajax Request

Leave a Reply

Your email address will not be published. Required fields are marked *