0

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 !

12
  • Show your code that you use to localize the script.
    – kaiser
    Mar 9, 2012 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, 2012 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'. Mar 9, 2012 at 10:39
  • 1
    Do you have something like Role Scoper installed?... I've had problems with AJAX and Role Scoper before now. Mar 9, 2012 at 10:41
  • 1
    @AjayPatel And why did it then work for admin users? URis are the same for everyone...
    – kaiser
    Mar 9, 2012 at 10:46

2 Answers 2

2

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.

0

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" ));
}
?>
1
  • 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. Mar 9, 2012 at 14:18

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.