最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugins - SOLVED - $wp_admin_bar and AJAX calls

matteradmin6PV0评论

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
  • 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 Commented Nov 12, 2018 at 16:41
  • hey @kero, thank you so much. I suppose that too, but how to achieve it then? Do you have an idea? – middlelady Commented Nov 12, 2018 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 Commented Nov 12, 2018 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? – middlelady Commented Nov 12, 2018 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 Commented Nov 12, 2018 at 18:16
 |  Show 1 more comment

1 Answer 1

Reset to default -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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far