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

How to restrict access to a single for users I’ve authorized? [closed]

You can do this without any plugins rather easily. If you’re only going to have a single page that needs to be accessed with an account created by you, try creating a page template with something like the below. Note: you should create any new account as a subscriber.

<?php 

    /*
        Template Name: Private Page
    */

    get_header(); 
?>
<?php 
    if( current_user_can('subscriber') {  ?> 
             // stuff here
<?php } else { 
    echo do_shortcode('[frontend-login-form]');
} ?>

<?php get_footer(); ?>

We have a shortcode in the example above, which comes from the code below which you should add to your functions.php file.

function frontend_user_manager_init() {
    add_shortcode( 'frontend-login-form', 'frontend_login_form' );
}
add_action('init', 'frontend_user_manager_init');
/**
 * Print a login form or current user login
 *
 * @param array $atts An array of arguments
 * @return string The form mark-up or the current user login
 */
function frontend_login_form( $atts ){
    if( ! is_user_logged_in() ){
        $args = array(
            'echo'          => false,
            'remember'      => true,
            'redirect'      => get_permalink(),
            'form_id'       => 'loginform',
            'id_username'       => 'user_login',
            'id_password'       => 'user_pass',
            'id_remember'       => 'rememberme',
            'id_submit'     => 'wp-submit',
            'label_username'    => __( 'Username' ),
            'label_password'    => __( 'Password' ),
            'label_remember'    => __( 'Remember Me' ),
            'label_log_in'      => __( 'Log In' ),
            'value_username'    => '',
            'value_remember'    => false
        );
        $output = wp_login_form( $args );
    }else{
        $current_user = wp_get_current_user();
        $output="<p>" . sprintf( __( 'Howdy %s' ), $current_user->user_login ) . '</p>';
        $output .= '<p>' . wp_loginout( get_permalink(), false ) . '</p>';
    }
    return $output;
}
// Process shortcodes in text widgets
add_filter('widget_text', 'do_shortcode');

The subscriber role has very limited access i.e. only allows the user to maintain their profile, however if you don’t want to allow this try adding the below code to your functions.php file.

add_action( 'init', 'blockusers_init' );
function blockusers_init() {
    if ( is_admin() && ! current_user_can( 'administrator' ) && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
        wp_redirect( home_url() );
        exit;
    }
}

Related Posts:

  1. Website Visible only to Registered users
  2. moving server can not login
  3. how to update current logged user username
  4. Restrict PDF links
  5. How can I allow access to two user accounts using one login?
  6. Redirecting or displaying a message on first login
  7. Password protect media attachment – share across guests
  8. WordPress login problem
  9. WordPress Login redirection according to user role
  10. Requiring login for specific pages
  11. Sign in with social media accounts without creating a WP account [closed]
  12. Require re-login when logged-in user attempts to access restricted page
  13. force login loophole
  14. URL Restrictions? Need only people who are logged in AND have a specific role (or roles) to access all pages for a site
  15. Custom user roles are unable to login
  16. How to show private pages based on a user’s role?
  17. How to password protect pages in WordPress
  18. How to lock WordPress front-end with login and password?
  19. Creating Custom Login Form Where Password Field is Dropdown Menu
  20. In Django, how do I know the currently logged-in user?
  21. Can I programmatically login a user without a password?
  22. Can’t log in: “ERROR: Cookies are blocked or not supported by your browser. You must enable cookies to use WordPress.”
  23. Is there any way to rename or hide wp-login.php?
  24. How to login with email only no username?
  25. How can I redirect user after entering wrong password?
  26. Increase of failed login attempts, brute force attacks? [closed]
  27. Login page ERROR: Cookies are blocked due to unexpected output
  28. Separate registration and login for different roles
  29. SSO / authentication integration with external ‘directory service’
  30. Preventing session timeout
  31. How reduce wordpress login session timeout time?
  32. How to restrict attachment download to a specific user?
  33. How to prefill WordPress registration with social details
  34. Check for correct username on custom login form
  35. Disallow user from editing their own profile information
  36. I can’t access my site via wp-admin
  37. ‘Password field is empty’ error when using autofill in Chrome
  38. Removing username from the ‘wordpress_logged_in’ cookie
  39. How to show ‘login error’ and ‘lost password’ on my template page?
  40. What is $interim_login?
  41. Custom login form
  42. How to prefill the username/password fields on the login page
  43. wp_signon returns user, but the user is not logged in
  44. Adding extra authentication field in login page
  45. Prevent wp_login_form() from redirecting to wp-admin when there are errors
  46. Redirect user using the ‘wp_login_failed’ action hook if the error is ’empty_username’ or ’empty_password’
  47. wp_signon() does not authenticate user guidance needed
  48. What exactly is ReAuth?
  49. What are the differences between wp_users and wp_usermeta tables?
  50. Login members using web services
  51. Make my wordpress blog remember my login “forever”
  52. How to check in timber if user is loggedin?
  53. How do I change the language of only the login page?
  54. Disable WordPress 3.6 idle logout / login modal window / session expiration
  55. Stop WordPress from logging me out (need to keep me logged in)
  56. Woocommerce registration page [closed]
  57. How to disable autocomplete on the wp-login.php page
  58. Share login data/cookies between multiple installations
  59. Synchronize WordPress user accounts across multiple domains and installations without using WordPress MU
  60. How to pass users back and forth using session data?
  61. How do I change the logo on the login page?
  62. Why does WordPress hide the reset password key from the URL?
  63. Is it possible to sign in with user_email in WordPress?
  64. How to use current_user_can()?
  65. Avoid to load default WP styles in login screen
  66. WordPress registration message
  67. How to fake a WordPress login?
  68. how to display the wordpress login and register forms on a page?
  69. Does wp_logout_url() destroy a session? (Logging out question)
  70. How can I send a welcome email to a user AFTER they login for the first time?
  71. Can not login with correct username and password
  72. How can i increase the login expiration length?
  73. How do I use add_action from a class method?
  74. How to remove the WordPress logo from login and register page?
  75. How can I add a custom script to footer of login page?
  76. Brute force attack?
  77. Customize wp_new_user_notification_email()
  78. Need to execute a cron job
  79. Login email after registration never sent or received
  80. How can I create a separate blog that is private?
  81. How to keep always logged in development environment
  82. Add Confirm Password field in wp-login.php Password Reset page
  83. Integrate recaptcha and wp_signon – what is needed?
  84. Stop users from logging in from multiple locations
  85. I want to disable E-Mail verifcation / activation when a user signs up for my WordPress site
  86. custom login page redirect to logged in user profile page
  87. Email address or username used to login in wordpress
  88. How do I check if a post is private?
  89. Front-end login: Redirect user to the post they had created
  90. Receiving “This content cannot be displayed in a frame” error on login page
  91. My login form does not work
  92. How to restrict a page [without plugin]
  93. Programmatically log in a wordpress user
  94. Action wp_login_failed not working if only one field is filled out
  95. Getting “Cookies are blocked or not supported by your browser” on login page
  96. What is the purpose of logging out after WordPress upgrade?
  97. Is it alright for two people to simultaneously be logged into a WP site as administrator?
  98. wp-login.php redirecting to HTTPS
  99. Display last login time
  100. How to customise wp-login.php only for users who are setting a password for the first time?
Categories login Tags content-restriction, login, user-access
What limits the maximum number of connections on a Linux server?
How to style bootstrap container in wordpress theme?

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