0
function __construct(){
        $ajax = add_action('wp_ajax_my_action', array($this, 'ajax_respone'));
        var_dump($ajax);
        var_dump($ajax == '1234');
        die();
    }

    public function ajax_respone(){
        $whatever = intval( $_POST['whatever'] );
        return $whatever;
    }

It prints:

bool(true)
bool(true)

How it is possible that '1234' or just 1234 is equal to true? Helps only ===

Also just noticed that the die() in next code doesn't work

function __construct(){
    add_action('wp_ajax_my_action', array($this, 'ajax_respone'));
}

public function ajax_respone(){
    $whatever = intval( $_POST['whatever'] );
    var_dump($whatever);
    die();
}

This means that the function will not start or it starts only when requested?

2
  • 3
    take a look at php.net/manual/en/types.comparisons.php and you will see why '1234' and 1234 are equal to true in Loose comparisons with ==
    – Bainternet
    Commented Dec 31, 2012 at 23:45
  • Well, yes - the function ajax_respone() will only execute when its called upon (in this case when you send a request to admin-ajax.php with the variable action set to "my_action" in the request data). How do you know that your call to die() isn't working?
    – bosco
    Commented Jan 1, 2013 at 0:13

1 Answer 1

1

add_action and add_filter always return true.

And yes, in PHP, any string that is not empty or "0" is loosely equal to the boolean true.

And I see nothing wrong with your die code. It's hooked to wp_ajax_my_action, so you need to make a call to the admin-ajax.php as an authenticated user, with the my_action request. Then it will die.

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.