Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a class that is doing some backend work ( enqueening css and js files creating metaboxes, registering ajax hooks etc...)

Everything works fine when I just include the files. The problem is that the files are included on every admin page.

In order to include them only on the page I really want ( CPT ) I've used the 'current_screen' admin hook.

Inside that hook it's safe to use 'get_current_screen' function

I use that function to determine on what page I'm currently on, so I can add appropriate classes. What happens next is that my class continues to work as expected ( enqueening css, js, creating metaboxes) only on CPT page, but ajax hooks stop working (response 0).

Ajax requests get triggered by clicking on the buttons inside the metaboxes.

I'm not really sure what's going on, there are no php errors.

  1. works:

     include(plugin_dir_path(__FILE__) .'/src/MyClass.php');
    
     new MyClass();
    
  2. broken:

    add_action('current_screen',"iki_admin_current_screen");
    
    function iki_admin_current_screen()
    {
    
       $screen = get_current_screen();
    
       if($screen->id == 'portfolio')
       {
          include(plugin_dir_path(__FILE__) .'/src/MyClass.pnp;
          new MyClass();
       }
    
    }
    
  3. broken:

    include(plugin_dir_path(__FILE__) .'/src/MyClass.
    add_action('current_screen',"iki_admin_current_screen");
    
    function iki_admin_current_screen()
    {
    
       $screen = get_current_screen();
    
       if($screen->id == 'portfolio')
       {
          new MyClass();
       }
    
    }
    

I would like to point out that the class works in all three examples, except for the AJAX calls, which work only on the first example.

share|improve this question
add comment

1 Answer

There is no current screen in the context of an AJAX request, only in the context of where you're making the AJAX calls from.

share|improve this answer
    
Can you please elaborate a little bit more? What is Ajax Context? I'm only creating the class via that hook. Ajax call is bound to the public method of the class. How would you approach this problem? –  iki_xx Sep 16 '13 at 17:22
    
The request for the portfolio admin page is entirely separate from the AJAX request you make from that page- two different, unrelated http requests. When the AJAX request is sent to the server, it doesn't pass your current screen test because it's not a request for the portfolio admin page, it's an AJAX request. add your AJAX handlers outside your current screen action. –  Milo Sep 16 '13 at 17:29
    
I think we don't understand each other correctly, current_screen hook is only used to determine on what page to include and instantiate the php class.That hook is fired after the admin_init hook. I'm not making multiple requests. Ajax request is sent when the user clicks on the button inside the metabox. –  iki_xx Sep 16 '13 at 17:35
    
I understand what you're doing, but you're not understanding my explanation. when someone clicks the button in your meta box, that's another request being sent to the server. when that request is processed by the server, your class is not present because your current screen action is not firing on that request, so your AJAX handlers don't exist. This is not unique to WordPress, this is how AJAX works. think of it like opening a separate browser window and opening a different page- one window is not "aware" of the state of the other window, they're two entirely separate requests. –  Milo Sep 16 '13 at 17:40
    
Okay, now I understand what you are saying. How would you solve this problem? Maybe include and instantiate the class outside the current_screen action, and create something like initmethod on the class, and trigger that method inside the action? init method would then enqueue necessary scripts, create metaboxes register ajax callbacks etc..? –  iki_xx Sep 16 '13 at 17:49
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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