Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Private page protected with username and password

So here’s what we’re trying to do:

IF A user logs in and is of a specific role ( Subscriber ) then redirect them to a specific page.

IF A user tries to reach the admin panel, redirect them to a specific page.

We’re going to use 3 hooks for the above problems:

  • login_redirect – Redirect user once they login
  • after_setup_theme – Remove admin bar
  • admin_init – Keeps subscribers out of admin panel

First, let’s create our redirect:

/**
 * Redirect subscribers to specific page
 * @param string $redirect
 * @param string $request
 * return string
 */
function member_redirect( $redirect, $request ) {
    if( current_user_can( 'subscriber' ) ) {
        return $specific_page
    }

    return $redirect;
}
add_filter( 'login_redirect', 'member_redirect', 10, 2 );

If the current user is a subscriber, we would replace the $specific_page with the page URL, otherwise let them go to where they were originally headed.

Next, let’s make sure subscribers can’t get to the admin panel:

/**
 * If user is subscriber AND trying to access admin panel, redirect them to specific page
 */
function subs_adminpanel_redirect(){
    if ( current_user_can( 'subscriber' ) ){
        wp_redirect( $specific_page );
        exit;
    }
}
add_action( 'admin_init', 'subs_adminpanel_redirect' );

Again, you could replace $specific_page with your actual URL.

Finally, let’s remove the admin bar so the user doesn’t even know there’s a dashboard.

/**
 * Remove adminbar for Subscribers
 */
function subs_remove_adminbar() { 
    if( ! current_user_can( 'subscriber' ) ) {
        add_filter( 'show_admin_bar', '__return_false' );   
    }
}
add_action( 'after_setup_theme', 'subs_remove_adminbar' );

This is pretty straight forward, if they’re a subscriber remove the admin bar for them.

A step shackleton pointed out in the comments below is by default, subscribers do not have access to view private pages. We need to add this as a capability:

/**
 * Allow Subscribers to view Private Pages
 */
function add_theme_caps() {
    $role = get_role( 'subscriber' );
    $role->add_cap( 'read_private_pages' );
}
add_action( 'admin_init', 'add_theme_caps' );

You would throw all this into your functions.php file at some place and test it out. Again, this only works for subscribers and only if you replace $specific_page with your actual page URL.

Related Posts:

  1. Lock out all WordPress Administrators except two specific users
  2. WordPress not logged in locally with correct username and password
  3. Can I protect a type of content site-wide with a single password?
  4. User login without username, only password
  5. Cannot login with correct username and password anymore
  6. Separate registration and login for different roles
  7. Check for correct username on custom login form
  8. Disallow user from editing their own profile information
  9. How do I require authorization / login to view a specific set of posts / pages?
  10. How can I create a separate blog that is private?
  11. Add Confirm Password field in wp-login.php Password Reset page
  12. How do I check if a post is private?
  13. How to customise wp-login.php only for users who are setting a password for the first time?
  14. Give visitor access to password protected page/post via external script
  15. Send reset password link to user from custom lost password form
  16. Is it possible to get a user with just the password field?
  17. How can I retrieve the username and password from my WordPress installation?
  18. password protect individual pages
  19. Problem with logging in WP users automatically
  20. How to determine if a user has not changed default generated password
  21. Restricting frontend acess based on user role otherwise redirect to login form
  22. Right practice to edit WP reset password email
  23. Deny a user role to log in after register
  24. reset password link redirect to login page
  25. Password protect media attachment – share across guests
  26. Password reset – Disabled for LDAP accounts
  27. How to integrate external user tables with WP?
  28. Allow Users Only Edit Their Profile?
  29. Why wp_update_user doesn’t update user_activation_key on users with apostrophes in their email?
  30. Forgot Password/ Password Reset Page does not exist
  31. Correct passwords keep appearing as incorrect
  32. Forgot password needs to redirect from wp-login to a custom page
  33. How can I change the email sender name from wordpress to (myblogname) on the “lost password” email?
  34. Set Default User Role
  35. How to password-protect everything except the logo
  36. Generate email on meta value update
  37. Is it possible to rebuild the website while not accesseing the original database?
  38. Is there anyway to get the inputted password string from the login form?
  39. Invalidate username if it contains @ symbol
  40. WordPress Login redirection according to user role
  41. Display first name instead of username
  42. How can I password protect a WordPress site without requiring users to log in?
  43. Password not resetting on wordpress?
  44. Prevent Subscriber Role to login
  45. autocomplete=”off” WordPress Login
  46. wordpress login without password just email address (NO 2 factor authentication with email)
  47. Password recovery URL has error – but not found in code or db
  48. Temporally disable password to login with empty password?
  49. I need to find which is the file that checks the DB for correct login (username, password)
  50. Login Based on ip
  51. URL Restrictions? Need only people who are logged in AND have a specific role (or roles) to access all pages for a site
  52. Make WordPress User Name be the Company Name when Registering (not the default ‘first name’ last name’ email address’)
  53. Login form does not store/remember/suggest users password
  54. Custom failed login error messages for users based on user role?
  55. Custom user roles are unable to login
  56. How to invalidate `password reset key` after being used
  57. WordPress password reset not working
  58. New user password confirmation sending wrong URL
  59. Locked out of WordPress admin area [closed]
  60. Global login to password protected pages
  61. Disable / Remove Password for Login WordPress
  62. How to password protect pages in WordPress
  63. Chosen user password in registration is not being accepted on Login
  64. wp_lostpassword_url not escaped
  65. Simplest way to create two private sections each with a common account
  66. Trouble logging in and/or changing password
  67. Redirect non-members to about/intro page
  68. Cant login, Password MUST be reset error, after reset
  69. Username character requirements
  70. Log in a user upon password reset?
  71. Customize From and email address on password reset
  72. In Django, how do I know the currently logged-in user?
  73. Can I programmatically login a user without a password?
  74. Can’t log in: “ERROR: Cookies are blocked or not supported by your browser. You must enable cookies to use WordPress.”
  75. Is there any way to rename or hide wp-login.php?
  76. How to login with email only no username?
  77. How can I redirect user after entering wrong password?
  78. Increase of failed login attempts, brute force attacks? [closed]
  79. Login page ERROR: Cookies are blocked due to unexpected output
  80. SSO / authentication integration with external ‘directory service’
  81. Preventing session timeout
  82. How reduce wordpress login session timeout time?
  83. Where to securely store API keys and passwords in WordPress?
  84. How to prefill WordPress registration with social details
  85. I can’t access my site via wp-admin
  86. ‘Password field is empty’ error when using autofill in Chrome
  87. Removing username from the ‘wordpress_logged_in’ cookie
  88. How to show ‘login error’ and ‘lost password’ on my template page?
  89. What is $interim_login?
  90. Custom login form
  91. How to prefill the username/password fields on the login page
  92. wp_signon returns user, but the user is not logged in
  93. How to protect pages with double authentication: password + email (in custom field)
  94. Adding extra authentication field in login page
  95. Replacing the WordPress password validation
  96. Prevent wp_login_form() from redirecting to wp-admin when there are errors
  97. Redirect user using the ‘wp_login_failed’ action hook if the error is ’empty_username’ or ’empty_password’
  98. wp_signon() does not authenticate user guidance needed
  99. What exactly is ReAuth?
  100. What are the differences between wp_users and wp_usermeta tables?
Categories login Tags login, password, private, user-roles, username
Feed, RSS not able to clear the cache and cannot change the limit of cache life time
Ajax load more posts – WP_Query parameters not working

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress