0

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.

  • I suppose the admin bar isn't rendered in AJAX calls, so admin_bar_load() will be loaded when you request the page, but not when making the ajax call – kero Nov 12 '18 at 16:41
  • hey @kero, thank you so much. I suppose that too, but how to achieve it then? Do you have an idea? – huraji Nov 12 '18 at 16:43
  • The request the admin bar is rendered in is not the same request that handled the AJAX request, you can't set a variable in one request then read it in another like that, that's not how PHP works – Tom J Nowell Nov 12 '18 at 16:49
  • Alright @TomJNowell but to manipulate the $wp_admin_bar I have 2 ways, as far as I know: wp_before_admin_bar_render and admin_bar_menu hooks. So do you say that I should hook those into the wp_ajax_ function? – huraji Nov 12 '18 at 16:58
  • 1
    Take a step back. Why do you want/need the admin bar in the AJAX call? You might have to test this, I'm not sure if simply setting show_admin_bar to true could solve this – kero Nov 12 '18 at 18:16
-1

That'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

  • This is a community that works on supportive answers, it wouldn't be an "exchange" otherwise. Giving lectures doesn't help anybody, even proven that you could know the code better than others. – huraji Nov 12 '18 at 17:03
  • @huraji every answer is in its way a mini lecture, that doesn't really help me improve the answer though, but I have tried to make it more concise. One could even argue the question should be closed as off topic as the root problem is fundamental to PHP not WP. If you want to discuss community and supportive answers further though, there's a meta stack for those kinds of discussions – Tom J Nowell Nov 12 '18 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.