Skip to main content
Bumped by Community user
added 28 characters in body
Source Link

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: {'action':'test2'}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();

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 = $.ajax({ 
                    url: ajaxurl,
                    type: "POST",
                    data: {'action':'test2'},
                    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();

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();
Fix code formatting, improve code indentation and grammar, misc
Source Link

Ajax call returns 0 when add_action is inside a class in functionfunctions.php

Ajax call returns 0 as output,. I am sure that hook is not working and itsit's not calling the test2test2 method. why add_action(.... Why is add_action(... not working inside class in function.php, please correct me.functions.php?

--------Wrap inside function.phpfunctions.php Starts-------

class test{

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 = 
                            $.ajax({    
                                url: ajaxurl,
                                type: "POST",
                                data: {'action':'test2'},
                                success: function(val) {
                                    alert(val);
                                },
                            }); 
                    });             
            </script><?php 
    }
                
    public function test2(){
        echo "success";
        exit;
    }       
}

}

--------Wrap inside function.phpfunctions.php Ends-------

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

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

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

Ajax call returns 0 when add_action is inside a class in function.php

Ajax call returns 0 as output, I am sure that hook is not working and its not calling the test2 method. why add_action(.... is not working inside class in function.php, please correct me.

--------Wrap inside function.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 = 
                            $.ajax({    
                                url: ajaxurl,
                                type: "POST",
                                data: {'action':'test2'},
                                success: function(val) {
                                    alert(val);
                                },
                            }); 
                    });             
            </script><?php 
    }
     
    public function test2(){
        echo "success";
        exit;
    }       

}

--------Wrap inside function.php Ends-------

Created a object in a template page and call to test1 method.

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

Ajax call returns 0 when add_action is inside a class in functions.php

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 = $.ajax({ 
                    url: ajaxurl,
                    type: "POST",
                    data: {'action':'test2'},
                    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();
Source Link

Ajax call returns 0 when add_action is inside a class in function.php

Ajax call returns 0 as output, I am sure that hook is not working and its not calling the test2 method. why add_action(.... is not working inside class in function.php, please correct me.

--------Wrap inside function.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 = 
                            $.ajax({    
                                url: ajaxurl,
                                type: "POST",
                                data: {'action':'test2'},
                                success: function(val) {
                                    alert(val);
                                },
                            }); 
                    });             
            </script><?php 
    }
    
    public function test2(){
        echo "success";
        exit;
    }       

}

--------Wrap inside function.php Ends-------

Created a object in a template page and call to test1 method.

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