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.
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?

share|improve this question
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 Dec 31 '12 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? –  boscho Jan 1 '13 at 0:13
add comment

1 Answer

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.

share|improve this answer
add comment

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.