2

i have simple wordpress form to add data in custom table in wordpress using Ajax

my Ajax code

jQuery.ajax(ajax_object.ajax_url, {
    type: "POST",
    data: data,
    cache: false,
    success: function (response) {
        alert(response);

    },
    error: function (error) {
        if (typeof console === "object") {
            console.log(error);
        }
    },
    complete: function () {
    }
});

my php code to save data

 if (!class_exists('bookly_appo_Ajax')) {
    class bookly_appo_Ajax
    {
        public function __construct()
        {
            add_action('init', array(&$this, 'init'));
        }

        public function init()
        {
            add_action('wp_enqueue_scripts', 'enqueue_ajax_booklyapp');
            function enqueue_ajax_booklyapp($hook)
            {

                wp_enqueue_script('ajax-script-booklyapp', plugins_url('/ajax.js?v=' . rand(), __FILE__), array('jquery'));
                wp_localize_script('ajax-script-booklyapp', 'ajax_object',
                    array(
                        'ajax_url' => admin_url('admin-ajax.php')
                    )
                );
            }

            add_action('wp_ajax_add_category_bookly', 'add_category_bookly_callback');
            function add_category_bookly_callback()
            {
                $storeid = $_REQUEST['storeid'];

                $rows = $wpdb->insert(
                    $table_category, array(

                        'store_id' => $storeid,
                    )
                );
                $lastid = $wpdb->insert_id;
            }
        }
    }
}

my question is

  1. when login with admin my ajax work fine but when login with other user(subscriber user) of my site it's give error "Opps!You do not have sufficient perissions to access this page"
  2. which type of accessibility provide to subscriber to used admin-ajax.php file
9
  • 1
    To add to Jordan Lejuwaan's correct answer, the absolute reference to $this in your init method is unneccessary, you can move enqueue_ajax_booklyapp() outside of it's parent function and if I'm not mistaken won't this code throw a parse error? You have the add_action outside of a function inside a class, you can put it in your init method. Commented Oct 3, 2014 at 16:23
  • class bookly_appo_Ajax is accessible for everybody? Are you sure you do not have a condition that filters users by role / capability?
    – aifrim
    Commented Oct 7, 2014 at 12:01
  • @aifrim yes class bookly_appo_Ajax is accessible for everybody. Not put any condition or filter for users by role/capability. Commented Oct 7, 2014 at 13:43
  • The error message "Opps! You do not have sufficient permissions to access this page" (presumed typos fixed) is not a WP message. You must be using a plugin or theme that is causing the problem.
    – bonger
    Commented Oct 7, 2014 at 14:17
  • 1
    @kaiser Or maybe it's from a plugin or theme!
    – bonger
    Commented Oct 11, 2014 at 14:46

1 Answer 1

5
+50

For non-admin users to be able to use an ajax function, you need to also include a second hook using wp_ajax_nopriv

So right after this:

add_action('wp_ajax_add_category_bookly', 'add_category_bookly_callback');

you should include this:

add_action('wp_ajax_nopriv_add_category_bookly', 'add_category_bookly_callback');

7
  • @ Jordan Lejuwaan add action wp_ajax_nopriv_add_category_bookly is not working. it's return same alert "Opps!You do not have sufficient perissions to access this page". any other solution? Commented Oct 6, 2014 at 5:06
  • 1
    but when admin user is login is working currently.. Commented Oct 6, 2014 at 5:07
  • @ankur - Where are you seeing that error? As an ajax response?
    – jetlej
    Commented Oct 7, 2014 at 3:51
  • 1
    When you call your jQuery.ajax function, what is in your data parameter? You would at a bare minimum need to provide { action: 'add_category_bookly' } in there somewhere... If you could provide a sample request header (in Chrome, with Developer Tools, you would find this under the Network tab), with sensitive info. like cookies scrubbed, it might be helpful. Commented Oct 7, 2014 at 12:34
  • 1
    Please copy/paste the full response you're getting from the Developer tools and put it in a pasteBin
    – jetlej
    Commented Oct 7, 2014 at 17:06

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.