Support » Plugins » Restricting subscribers to admin area by redirect cause AJAX problem

  • Hi,

    I am working on website in which i don’t want to display my admin area of website to user whose role is “Subscriber“.

    After doing some R & D on wordpress i got following code which redirects user with role “Subscriber” to specified redirect url.

    add_action( 'init', 'blockusers_init' );
    function blockusers_init() {
      if (!current_user_can( 'manage_options' )) {
        if (is_admin()) {
            wp_redirect( home_url() );
            exit;
        }
      }
    }

    This works for redirection but issue due that occur is my ajax is not working. Because i have implemented ajax in website in wordpress style.

    In which i am sending ajax to particular function using wp-admin/admin-ajax.php. Now due to that redirection affects to WordPress ajax and my request of ajax moved to another url.

    Is anybody know how to restrict admin area in proper way for subscriber with wp-ajax need to work properly.

    Please see Screenshot here.

    I will be happy to hear your opinions

    Thanks & Regrads
    Omprakash

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi OP,

    Just check out this below link, would help to solve your problem
    http://codex.wordpress.org/Plugin_API/Action_Reference/admin_init#Example:_Access_control

    Regards,
    Joel James

    Great thanks Joel Now its working properly. I just made following changes to my function are:

    Before:

    add_action( 'init', 'blockusers_init' );
    function blockusers_init() {
      if (!current_user_can( 'manage_options' )) {
        if (is_admin()) {
            wp_redirect( home_url() );
            exit;
        }
      }
    }

    After:

    add_action( 'init', 'blockusers_init' );
    function blockusers_init() {
      if (!current_user_can( 'manage_options' )&& $_SERVER['PHP_SELF'] != '/wp-admin/admin-ajax.php') {
        if (is_admin()) {
            wp_redirect( home_url() );
            exit;
        }
      }
    }

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Restricting subscribers to admin area by redirect cause AJAX problem’ is closed to new replies.