我的课堂上发生了一些奇怪的事情。基本上我是想操纵$wp_admin_bar 通过AJAX,但当我收到响应时,我得到一个空值。虽然在初始化方法时调试全局,但我得到了正确的值。
我的班级:
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);
   }
}
 在我的页面中,使用操作调用
get_the_page() 通过AJAX,在
$(document).ready 然后我放置了:
<?php var_dump($default_bar); ?>
 编辑/解决方案我意识到电话有时间问题。我只是改变了我的课堂逻辑。