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.

I'm working on an ajax request, I have several ajax requests on my functions.php. And all are working, but this:

On my php I have this:

add_action("wp_ajax_fb_points", "get_fb_points");
add_action("wp_ajax_nopriv_fb_points", "get_fb_points");

function get_fb_points(){
    if ( !wp_verify_nonce($_REQUEST['nonce'], 'fb_points_nonce')) {
        exit('No naughty business');
    }
    echo 'yeah';
    die();
}

On the footer.php I have this:

FB.Event.subscribe('edge.create',
    function(response) {
        if(response == '<?php echo get_facebook_profile('link'); ?>') {
            $.ajax({
                url: '<?php echo admin_url('admin-ajax.php'); ?>',
                data: { action: 'fb_points', challenge : 2, nonce : '<?php echo wp_create_nonce('fb_points_nonce'); ?>' },
                async: false,
                success: function(data){
                    console.log(data);
                }
            });
        }
    }
);

But I don't know why it isn't working the request response is "No naugty business". Can anyone help me to find out what is wrong?

share|improve this question
    
Can you posted the $_REQUEST['nonce'] and wp_create_nonce('fb_points_nonce') values? –  Rilwis Sep 6 '12 at 5:54
    
Thanks Rilwis, I noticed that the problems was with the get_current_user_id(). I don't know why but I changed with wp_get_current_user() and it solves de problem. –  jepser Sep 6 '12 at 6:34
    
@jepser Where is your wp_get_current_user() function in the above? Usually, the nonce verification would be at the top of the script so I'm not sure where you went wrong. –  AlxVallejo Dec 27 '12 at 16:04
add comment

2 Answers 2

I don't know I just submitted the ticket on track. I was using get_current_user_id() and it modifies the wp_create_nonce behavior.

Sinces wp_create_nonce uses wp_get_current_user() I think there is the problem.

share|improve this answer
1  
get_current_user_id is a wrapper for wp_get_current_user. wp_create_nonce uses the user id as part of the hash so the method of getting the id makes no difference as long as both methods return the same id. I don't see your ticket can you post a link to it. –  Chris_O Sep 6 '12 at 5:42
    
Hi Chris, anyway to check "my tickets" I can't find it out in trac. Thanks for your help. –  jepser Sep 6 '12 at 6:34
add comment

When passing data using ajax I prefer to use check_ajax_referer instead of wp_verify_nonce.

B.Event.subscribe('edge.create',
    function(response) {
        if(response == '<?php echo get_facebook_profile('link'); ?>') {
            $.ajax({
                url: '<?php echo admin_url('admin-ajax.php'); ?>',
                data: { action: 'fb_points', challenge : 2, _ajax_nonce : '<?php echo wp_create_nonce('fb_points_nonce'); ?>' },
                async: false,
                success: function(data){
                    console.log(data);
                }
            });
        }
    }
);

function get_fb_points(){
    check_ajax_referer( 'fb_points_nonce');
    echo 'yeah';
    die();
}

I believe your method is not working because you are using $_REQUEST instead of $_POST.

function get_fb_points(){
    $nonce = $_POST['nonce'];
    if ( !wp_verify_nonce( $nonce, 'fb_points_nonce')) {
        exit('No naughty business');
    }
    echo 'yeah';
    die();
}
share|improve this answer
    
I used wp_create_nonce('add_list') on a page named "Profile" and output this: cccebe67d7 and then in other page output: bc059709c7. As I know the nonces have to be the same. What do you think is the problem? –  jepser Sep 6 '12 at 5:01
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.