0

Here is summary of the problem and required solution:

  • Access to mywebsite.com/wp-admin is blocked for subscribers [which is good]

  • However, if i enter the link manually on the browser as follows: https://mywebsite.com/wp-admin/user-edit.php?user_id=113 then the user has access to his user settings

  • Problem with that is that they can then create an API key (through application passwords plugin which is accessible from that page). This is undesirable as I dont want the users to have API keys where they can fetch/post data to server.

  • Hence, I want to block access of subscribers to all wp-admin menus/plugin pages including this link https://mywebsite.com/wp-admin/user-edit.php?user_id=113

Any suggestions?

8
  • 1
    "Access to mywebsite.com/wp-admin is blocked for subscribers" - how did you do that? Using a plugin, or custom code - and if so, what code? Also, is the REST API on your site restricted to logged-in/authenticated users only (for all operations/endpoints, e.g. read/fetch and write/create a post)?
    – Sally CJ
    Commented Apr 11, 2022 at 12:50
  • The rest api is restricted for authenticated users. The authentication is handled using WP rest api authentication plugin. Which requires a api key that is generated by application passwords
    – The Oracle
    Commented Apr 11, 2022 at 13:31
  • 1
    So, it's the plugin which restricts the REST API access to authenticated users only; or do you use any code for that? And I'm just wondering, why do you still need the plugin when application passwords is now a core feature in WordPress? And as for this question, are you sure you want subscribers to not be able to access any admin pages, including the profile editing page? Is it just because of the fact that they're able to generate application passwords via the admin page?
    – Sally CJ
    Commented Apr 11, 2022 at 14:20
  • 1
    Ok, after deleting the authentication plugin, the api key is working without needing to use the application passwords plugin. However, the rest api is now accessible by everyone. Are there any resources that can be recommended to include rest api authentication via php in wordpress. I did not find online. Much appreciated.
    – The Oracle
    Commented Apr 11, 2022 at 17:08
  • 1
    See my answer, but I included the first snippet (or the very first 4 paragraphs) just for completeness since that's what originally asked in the question.
    – Sally CJ
    Commented Apr 11, 2022 at 17:26

2 Answers 2

3

Hence, I want to block access of subscribers to all wp-admin menus/plugin pages including this link https://mywebsite.com/wp-admin/user-edit.php?user_id=113

This isn't a bulletproof solution, but it should work in that non-admin users would no longer be able to access any admin pages when they're logged-in:

add_action( 'admin_init', function () {
    if ( wp_doing_ajax() || ! is_user_logged_in() ) {
        return;
    }

    $roles = (array) wp_get_current_user()->roles;
    if ( ! in_array( 'administrator', $roles ) ) { // allows only the Administrator role
        wp_die( 'Sorry, you are not allowed to access this page.' );
        // or you can redirect the user to somewhere, if you want to
    }
} );

But then, you might want to change the login and registration redirect URL so that it doesn't send the user to an admin page upon successful login/registration — see the documentation for login_redirect and registration_redirect.

Problem with that is that they can then create an API key (through application passwords plugin which is accessible from that page).

I can't help you with that plugin, but unless if you're still using WordPress prior to v5.6.0, then you should not need to use a plugin anymore because application passwords has been a core feature in WordPress since v5.6.0. And there's actually a hook named wp_is_application_passwords_available_for_user that you could use to disable the feature for certain users.

This is undesirable as I dont want the users to have API keys where they can fetch/post data to server.

If so, and since you said in your comment, "The rest api is restricted for authenticated users", then how about using the rest_authentication_errors hook to ensure only Administrators allowed to access the REST API?

Working example:

add_filter( 'rest_authentication_errors', function ( $errors ) {
    if ( ! is_wp_error( $errors ) ) { // do nothing if there's already an error
        if ( $can_access = is_user_logged_in() ) {
            $roles      = (array) wp_get_current_user()->roles;
            $can_access = in_array( 'administrator', $roles ); // allows only the Administrator role
        }

        if ( ! $can_access ) {
            return new WP_Error( 'user_not_allowed',
                'Sorry, you are not allowed to access the REST API.',
                array( 'status' => rest_authorization_required_code() )
            );
        }
    }

    return $errors;
} );
2
  • 1
    Everything Working fine now. I have deleted application passwords and rest authentication plugins and instead im using the built in application passwords. To disallow api key generation for subscribers i used the wp_is_application_passwords_available_for_user hook. To authenticate users i used suggested code for rest_authentication_errors hook. Thanks Sally.
    – The Oracle
    Commented Apr 11, 2022 at 17:46
  • You're very welcome :)
    – Sally CJ
    Commented Apr 11, 2022 at 17:56
-1

Add this code to your theme’s functions.php file or in a site-specific plugin:

function restrict_access_admin_panel(){
    global $current_user;
    get_currentuserinfo();
 
    if ($current_user->user_level <  4) {
        wp_redirect( get_bloginfo('url') );
        exit;
    }
}
add_action('admin_init', 'restrict_access_admin_panel', 1);

source: https://www.isitwp.com/restrict-wp-admin-access-to-subscribers/

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.