0

Just curious - whether AJAX Action can be hooked into wp hook?
For Example:
add_action( 'wp', array( 'MyAjaxRegistrarClass', 'ajax_registrar' ) );

// MyAjaxRegistrarClass
class MyAjaxRegistrarClass{

    public static function ajax_registrar() {       
        MyAnotherClass::fn_register_ajax();
        //Call fn_register_ajax of other classes here
    }
}//Class Ends here

// MyAnotherClass
class MyAnotherClass {

    public static function fn_register_ajax() {     
        add_action( 'wp_ajax_testing_ajax', array(__CLASS__, 'testing_ajax' ) );
    }   

    public static function testing_ajax() {     
        echo 'Success!'.__FUNCTION__.' was called from '.__CLASS__;     
        die();
    }   
}

Any pointer? Of-course, there are many options for Ajax Hooks. Just curious to know whether this can be done or not.

  • I don't even remember there being wp hook, what exactly do you want to achieve with it and for what purpose? :) – Rarst Oct 8 '15 at 14:28
  • There is indeed a wp hook (codex.wordpress.org/Plugin_API/Action_Reference/wp), but I don't know whyy you'd want to do this? – vancoder Oct 8 '15 at 21:24
  • I just wanted to register my ajax actions after WP object has been created. Because after WP I can identify post/page based on which I would register ajax actions. Note: I wanted to register ajax actions only when a particular page/custom post type post loads. – Niraj Kumar Oct 16 '15 at 10:59
0

Apparently not! My design goal was to automate the creation of related objects while performing ajax options. However, I have modified it to automate the same task differently.

  • Now all ajax functions in my classes are static.
  • JavaScript captures the class name and function name where the ajax processing will happen (as these are provided as JavaScript variables programmatically) and send the same with ajax post/get.
  • Get/Post data gets captured by a universal ajax processor and determines which class::function to call.

So, considering this, all I have to do is create just one ajax_action hook for my universal ajax processor.

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.