0

Ajax call returns 0 as output. I am sure that hook is not working and it's not calling the test2 method. Why is add_action(... not working inside class in functions.php?

--------Wrap inside functions.php Starts-------

class test{
    public function __construct() {
        add_action( 'wp_ajax_test2', array( $this, 'test2' ) );
    }                       
    public function test1() { ?>
        <script>
            jQuery(document).ready(function($){
                var ajaxurl = "<?php echo get_site_url();?>/wp-admin/admin-ajax.php";
                var data = {'action':'test2'};
                 $.ajax({   
                    url: ajaxurl,
                    type: "POST",
                    data: data,
                    success: function(val) {
                        alert(val);
                    },
                }); 
            });             
        </script><?php 
    }               
    public function test2(){
        echo "success";
        exit;
    }       
}

--------Wrap inside functions.php Ends-------

Created an object in a template page and call to test1 method:

$ob_call = new test;
$ob_call->test1();
5
  • How does your object get created when the ajax call is made? It's not the same request you are making the call from.
    – Milo
    Commented Sep 16, 2015 at 16:36
  • Thanks for your response....i am not creating my object in ajax call, its created in my pages (Template). $ob_call = new test; is located in my page when i am viewing my page its gets created
    – fernandus
    Commented Sep 21, 2015 at 6:15
  • When your AJAX call is made there is no function hooked to that action, because there is no object. Your page and your AJAX call are two separate requests with no data persisting between the two. You must add your object on every request within your functions.php file.
    – Milo
    Commented Sep 21, 2015 at 16:00
  • Thanks milo....it works i have posted the answer below...
    – fernandus
    Commented Sep 22, 2015 at 9:24
  • wordpress.stackexchange.com/questions/203383/…
    – fernandus
    Commented Sep 22, 2015 at 10:26

3 Answers 3

1

While I am not sure why you want to do it like this, please find below the working code. Please note the comments in line. I hope this helps.


class test{
    public function __construct() {
        add_action( 'wp_ajax_test2', array( $this, 'test2' ) );
        /* Front end ajax needs this. */
        add_action( 'wp_ajax_nopriv_test2', array( $this, 'test2' ) );
        add_action( 'wp_enqueue_scripts', array( $this, 'test1' ) );
    }

    public function test1() {
      /* in JavaScript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value */
      wp_localize_script('test-script-ajax', 'ajaxobj', array('ajaxurl' => admin_url('admin-ajax.php')));
      /* Moved your js to a separate js file and enquing it the WordPress way */ 
      wp_enqueue_script( 'test-script-ajax', get_stylesheet_directory_uri() . '/js/test-ajax.js', array( 'jquery' ));
    }

    public function test2(){
        echo "success";
        exit;
    }
}

$ob_call = new test;
$ob_call->test1();

Here is the code in test-ajax.js file


jQuery(document).ready(function($){
  var data = { 'action':'test2' };
  jQuery.post( ajaxobj.ajaxurl, data, function( response ) {
            alert(response);
    });
});
7
  • >>Hi bro thanks for your response.... its not helping me, i have tried this sets of codes, Ajax call returns 0 its not returning success as i told before its not referring to test2 method. >>Why i am not using wp_enqueue_script(... is that it takes java script is external js file and i cant use php. The same sets of code i tried to implement in plugin its working pretty good. >>I want to know why its not working in function.php and i want to implement it in function.php. >>I have posted the cods which i used it in plugin below, no space so i posted it in (Answer Your Question)
    – fernandus
    Commented Sep 18, 2015 at 6:14
  • class test{ public function __construct() { add_action( 'admin_menu', array( $this, 'pro_ad )); add_menu_page( 'Pro-Cl', 'Pro-Cl', 'appo', 'pro-reg', array($this,'t1')); } public function t1(){?> <script>jQuery(document).ready(function($){ var ajaxurl = "<?php echo get_site_url();?>/wp-admin/admin-ajax.php"; var data = $.ajax({ url:ajaxurl, type:"POST", data:{'action':'t2'}, success:function(a) {alert(a);} }); }); </script><?php } public function t2(){ echo "1"; exit; } } new t;
    – fernandus
    Commented Sep 18, 2015 at 6:24
  • Can you please update your comment above so that its readable? Commented Sep 18, 2015 at 6:55
  • i had reached the maximum character provided for comments so i cant make the content clear, is there any alternative way to update it clearly....
    – fernandus
    Commented Sep 21, 2015 at 6:06
  • which one is not clear to you first or second comment.
    – fernandus
    Commented Sep 21, 2015 at 6:07
0

Add wp_die(); at the end the your function.

1
  • If you're already echoing something shouldn't the exit be enough? What's the benefit of using wp_die() instead?
    – Rup
    Commented Dec 8, 2021 at 20:44
0

It works now

----------Function.php---------------

add_action( 'wp_ajax_test2', array( 'test', 'test2' ) );

class test{

    public function __construct() {
        
    }                       
    public function test1() { ?>
        <script>
            jQuery(document).ready(function($){
                var ajaxurl = "<?php echo get_site_url();?>/wp-admin/admin-ajax.php";
                var data = {'action':'test2'};
                 $.ajax({ 
                    url: ajaxurl,
                    type: "POST",
                    data: data,
                    success: function(val) {
                        alert(val);
                    },
                }); 
            });             
        </script><?php 
    }               
    public function test2(){
        echo "success";
        exit;
    } 
          
}

------------------page (Template)-------------

$ob_call = new test;

$ob_call->test1();

Have a look to https://wordpress.stackexchange.com/questions/203383/why-hooking-differs-in-plugin-and-function-php-wordpress

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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