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.

All

In my application there is notification system, when user click on that icon I want to make ajax call. The problem id it works fine for admin user (Debug : 200 ok), but not for subscriber user (Debug : 301 moved permanently).

Ajax call

$("#notifications-button").click(function() {


    $.ajax({
    type : 'POST',

    url: '<?php echo get_option('siteurl') . '/wp-admin/admin-ajax.php' ?>',
    data:'action=my_special_ajax_call&value=1',
    success : function(data){
        $('.message-count').hide();
    },

    });
});

Function

 function implement_ajax() {

if(isset($_POST['value']))
  {

    global $wpdb, $user_ID;

    $wpdb->query( $wpdb->prepare("UPDATE wp_frm_items SET alerts_flag = 0 WHERE user_id = '". (int)$user_ID ."'" ));
}
}
add_action('wp_ajax_my_special_ajax_call', 'implement_ajax');
add_action('wp_ajax_nopriv_my_special_ajax_call', 'implement_ajax');//for users that are not logged in.

I do not able to get why this not working for subscriber? Is there anything wrong in code?

Thanks In Advance !

share|improve this question
    
Show your code that you use to localize the script. –  kaiser Mar 9 '12 at 10:35
    
I am not making any kind of separation for specifc user , i just wanted to work this for all –  Ajay Patel Mar 9 '12 at 10:38
1  
Not a solution to your question but it would be better to use admin_url('admin-ajax.php') rather than get_option('siteurl') . '/wp-admin/admin-ajax.php'. –  Stephen Harris Mar 9 '12 at 10:39
1  
Do you have something like Role Scoper installed?... I've had problems with AJAX and Role Scoper before now. –  Stephen Harris Mar 9 '12 at 10:41
1  
@AjayPatel And why did it then work for admin users? URis are the same for everyone... –  kaiser Mar 9 '12 at 10:46
show 7 more comments

2 Answers

up vote 2 down vote accepted

The problem is a conflict with the plug-in Role Scoper. Deactivating the plug-in resolves the issue. As an alternative to Role Scoper, (I do not offer this as a recommendation, simply a suggestion), there is the plug-in Members.

share|improve this answer
add comment

Solving by make manual ajax call to test.php page

<?php 

ini_set('html_errors', 0);
define('SHORTINIT', true);

require '../../../wp-load.php';
require( ABSPATH . WPINC . '/formatting.php' );
require( ABSPATH . WPINC . '/meta.php' );
require( ABSPATH . WPINC . '/post.php' );
wp_plugin_directory_constants();

// stuff goes here

if(isset($_POST['value']))
  { 

    $uid=$_POST['value'];
    global $wpdb;
    $wpdb->query( $wpdb->prepare("UPDATE wp_frm_items SET alerts_flag = 0 WHERE user_id = $uid" ));
}
?>
share|improve this answer
    
You'll want to do some nonce checks (and perhaps permission checks), and also, look up how to use $wpdb->prepare. Right now the above would leave your database open to attack. –  Stephen Harris Mar 9 '12 at 14:18
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.