Something weird is happening in my class. Basically I'm trying to manipulate the $wp_admin_bar
through AJAX but when I receive the response I get a NULL value.
Although debugging the global when my method is initialised, I get the right values.
My class:
class my_class(){
var $DefaultBar;
public function __construct(){
$this->initialise();
}
public function initialise(){
add_action( 'wp_before_admin_bar_render', array( $this , 'admin_bar_load' )); //or admin_bar_menu
add_action( 'wp_ajax_get_the_page', array( $this ,'get_the_page') );
}
public function admin_bar_load(){
global $wp_admin_bar;
//here I assign the value to the class variable declared before
$GLOBALS['default_bar'] = $wp_admin_bar;
//if I var_dump here, I get all the values
}
public function get_the_page(){
global $default_bar;
ob_start();
include 'inc/forms/my_page.php';
$response = ob_get_contents();
ob_end_clean();
wp_send_json($response);
}
}
In my page, called with action get_the_page()
through AJAX, on $(document).ready
I've placed then:
<?php var_dump($default_bar); ?>
EDIT / SOLUTION
I realised there's a timing issue about calls. I just changed the logic of my class.
Something weird is happening in my class. Basically I'm trying to manipulate the $wp_admin_bar
through AJAX but when I receive the response I get a NULL value.
Although debugging the global when my method is initialised, I get the right values.
My class:
class my_class(){
var $DefaultBar;
public function __construct(){
$this->initialise();
}
public function initialise(){
add_action( 'wp_before_admin_bar_render', array( $this , 'admin_bar_load' )); //or admin_bar_menu
add_action( 'wp_ajax_get_the_page', array( $this ,'get_the_page') );
}
public function admin_bar_load(){
global $wp_admin_bar;
//here I assign the value to the class variable declared before
$GLOBALS['default_bar'] = $wp_admin_bar;
//if I var_dump here, I get all the values
}
public function get_the_page(){
global $default_bar;
ob_start();
include 'inc/forms/my_page.php';
$response = ob_get_contents();
ob_end_clean();
wp_send_json($response);
}
}
In my page, called with action get_the_page()
through AJAX, on $(document).ready
I've placed then:
<?php var_dump($default_bar); ?>
EDIT / SOLUTION
I realised there's a timing issue about calls. I just changed the logic of my class.
Share Improve this question edited Nov 14, 2018 at 9:19 middlelady asked Nov 12, 2018 at 16:12 middleladymiddlelady 5531 gold badge6 silver badges18 bronze badges 6 | Show 1 more comment1 Answer
Reset to default -1That's not how PHP requests work, you cannot set a variable in one request, then read it in another request. It isn't like a Node or Python application where the program runs continuously in the background, every request is a blank slate.
So, to make it work, in the first request you need to put it somewhere that the second request can read from, for which there are several options:
- Cookies
- User meta
- Options/Transients
As it's a logged in user, and persisting across sessions would make sense, user meta seems the most logical choice, via update_user_meta
and get_user_meta
admin_bar_load()
will be loaded when you request the page, but not when making the ajax call – kero Commented Nov 12, 2018 at 16:41$wp_admin_bar
I have 2 ways, as far as I know:wp_before_admin_bar_render
andadmin_bar_menu
hooks. So do you say that I should hook those into the wp_ajax_ function? – middlelady Commented Nov 12, 2018 at 16:58show_admin_bar
totrue
could solve this – kero Commented Nov 12, 2018 at 18:16