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

Login/password protected “client page”

I am going to recommend you reorganize your thinking. Instead of having posts generate a user login (which is complicated, but can be done), I think you can associate a post to a user.

Unfortunately, it doesn’t seem there are any plugins that will do this for you. I wrote this quick plugin you can use to get you started. This has not been tested, so use at your own risk and be careful.

The plugin outlined below does two things:

  1. Adds a field to all user profiles for the administrator allowing you to tie a user to a private post. Note: This only shows private posts, so the post in question must be private.
  2. Detects a page with a slug “client” and redirects the user to their private post if the administrator has set their private post in their profile.

Steps to install:

  1. Upload the above into the plugins folder: /wp-content/plugins/your-custom-plugin/custom-plugin.php.
  2. Login to wp-admin, activate “Custom Client-Post Login”.
  3. Create a page called “Client”.
  4. Create a private post (e.g., “Client A’s Private Post”).
  5. Create or edit a user login for your client. Scroll to the bottom of their profile and you’ll see “Client Post Association”. Choose the post you want them to see when visiting example.com/client/
  6. Login as your client and test by visiting example.com/client/

Be sure to comment with any questions you may have.

<?php

/*
Plugin Name: Custom Client-Post Login
Description: Each user as a private post linked to it. When visiting /clients, they are redirected.
Version: 1.0
*/

/*
Setup Steps:
1. Create page called "Client".
2. Create two or three posts that are marked as "PRIVATE".
3. Create two or three users.
4. When editing users, you can set their custom post in their user profile.
5. When that user is logged in, and visits the client page, they will be redirected to their private post.
*/


/*  
CODE FROM https://developer.wordpress.org/plugins/users/working-with-user-metadata/#example-form-field
*/

/**
 * The field on the editing screens.
 *
 * @param $user WP_User user object
 */
function sfusrcust_usermeta_form_field_client_post($user){
    $options = "";

    $chosen_post_id = get_user_meta($user->ID, '_client_post', true);

    $posts = get_posts(array(
        'post_type' => 'post',
        'post_status' => 'private',
        'showposts' => '-1'
    ));

    if(count($posts) > 0){
        foreach($posts as $post){
            $options .= sprintf('<option value="%s" %s>%s</option>', $post->ID, ($chosen_post_id ? 'selected': ''), $post->post_title);
        }
    }

    if(!current_user_can('administrator')){
        echo sprintf('<input type="hidden" name="client_post" id="client_post" value="%s" />', $chosen_post_id);
        return;
    }
    ?>
    <h3>Client Post Association</h3>
    <table class="form-table">
        <tr>
            <th>
                <label for="client_post">Select the users post.</label>
            </th>
            <td>
                <?php if($options !== ''){ ?> 
                <select name="client_post" id="client_post">
                    <?php echo $options; ?>
                </select>
                <?php } else { echo 'No private posts were found.'; } ?>

                <p class="description">
                    This allows you, the administrator, to select the post that the client will see when visiting the client page.
                </p>
            </td>
        </tr>
    </table>
    <?php
}

add_action('edit_user_profile', 'sfusrcust_usermeta_form_field_client_post');
add_action('show_user_profile', 'sfusrcust_usermeta_form_field_client_post');
add_action('personal_options_update', 'sfusrcust_usermeta_form_field_client_post_update');
add_action('edit_user_profile_update', 'sfusrcust_usermeta_form_field_client_post_update');
function sfusrcust_usermeta_form_field_client_post_update($user_id)
{
    // check that the current user have the capability to edit the $user_id
    if (!current_user_can('edit_user', $user_id)) {
        return false;
    }

    // create/update user meta for the $user_id
    return update_user_meta(
        $user_id,
        '_client_post',
        $_POST['client_post']
    );
}



/*
Code From: https://developer.wordpress.org/reference/hooks/pre_get_posts/
*/

add_action( 'pre_get_posts', 'pre_get_posts_client_page' );
function pre_get_posts_client_page( $query ) {
    /*
    NOTE: The only reason this works is because of the "client" slug. If you need to change that, you will have to change the slug below as well.
    */
    if ( ! is_admin() && is_user_logged_in() && is_page('client') ) {
        $user_post_id = get_user_meta(get_current_user_id(), '_client_post', true);
        if($user_post_id){
            $query->set('p', $user_post_id);
        }

    }
}

Related Posts:

  1. login to wordpress with Get variables instead of Post
  2. How to create a word press user with hashedpassword
  3. Is there any good tutorial to write custom login, registration and password recovery forms? [closed]
  4. Lost Password of my site, how to reset wordpress password?
  5. Login with OpenID, similar to Stack Exchange sites?
  6. How can I make an Ajax login form work with FORCE_SSL_ADMIN enabled?
  7. Change success message in plugin Theme my login
  8. How do I email a new page password to somebody every month?
  9. How to get Login Error messages on a custom template
  10. How can a Firebase user registration and login be integrated into a WordPress site?
  11. How to get user-meta from Social Login registered users?
  12. Auto login using Active Directory and Windows Authentication
  13. Which hook should be used to validate custom form fields on the login form?
  14. Add new password rule to Ultimate Member register form
  15. How can I make content disappear when a user logs in?
  16. How to create custom LOGIN and REGISTRATION forms?
  17. Tracking last login and last visit
  18. Multiple Password Portal Page BEFORE User Account Set Up
  19. Is it possible to block subscriber users to changing its password?
  20. Prevent Brute Force Attack
  21. Problem protecting a page with a password
  22. Login with email (WP Modal Login)
  23. Check if the front end user is log in or not
  24. wordpress custom login successful redirect hook
  25. WordPress with CAS+LDAP and standard WP accounts
  26. Disable WordPress password reset via mails,instead notify admin about the reset request
  27. Use WordPress with a custom OAuth2 provider
  28. WordPress login with Phone Number [closed]
  29. Cannot access wp-admin after disabling all plugin
  30. Preventing BFA in WordPress without using a plugin
  31. wp_authenticate but not logged in
  32. Force [wordpress_social_login] shortcode to display where it is embedded [closed]
  33. Cannot login to ADMIN even after changing password in phpmyadmin
  34. How do i login when i cant access wp-login.php?
  35. Why is my staging subdomain not sending wordpress_logged_in cookies?
  36. Single central login for front end users from any site
  37. How to delete Passwrd Protected posts cookies when a user logged out from the site
  38. How can I force users to a particular subdomain to log in for MU (Multisite)?
  39. Plugin: Google Analytics for Dashboard error – Timestamp is too far from current time
  40. “Request has expired” with “Make your site social” (Gigya) plugin
  41. How to save generated JWT token to cookies on login?
  42. Contributive page where people logged in can write
  43. Login cookies set as wrong domain
  44. How to use login_redirect with a user capability
  45. Login Customizer doesn’t change the background of the register form
  46. easy steps to make front end form without plugin
  47. Completely disabling password reset/recovery
  48. Change wp-login to custom URL login page
  49. Login problem after installing my written plugin [closed]
  50. Specific way to allow WordPress users to view their current password? And edit it?
  51. Too many login attempts
  52. Get ‘Headers already sent’ error for the plugin I am creating when I try to login
  53. Custom Login Page — wp_signon Headers Already Sent?
  54. Theme My Login Shortcode Doesn’t Return Anything
  55. Possibility to login without password
  56. how do i change my website facebook login button to another text immediately user login? [closed]
  57. WordPress unable to write files in the server
  58. Custom PHP Page Using WordPress login
  59. Plugin: connect to external database without showing password
  60. How to Use the Filter “sidebar_login_widget_form_args”
  61. Manage PDF downloads and protected pages
  62. If I use an alternative login (e.g. CAS or other SSO) plugin, is my site protected from the recent brute force login attempts?
  63. login in wordpress using gmail account
  64. How can I replace content on site generated from plugin without changing plugin
  65. Janrain/Simple Modal under Redirected Domain
  66. Linking form to user meta fields
  67. WordPress Multisite Profile Picture Sync Error with Nextend Social Login Plugin
  68. Plugin or ways to limit number of users logging in the website,
  69. force logged in user to stay in the dashboard
  70. Create Woocommerce account password post-checkout on thank you page
  71. Share login credential with QR code
  72. Which membership plugin for a simple sign in? Personal areas for customers
  73. Discern a specific plugin’s action hooks
  74. Automatic chage password of pages after some time
  75. wp_set_password() does not work!
  76. WordPress Admin login redirect to homepage
  77. Login issue in WordPress
  78. How to make a page both “private” and “password protected”
  79. Password Protect wp-content?
  80. https rewrite not working for All in one security Brute force > rename login url
  81. On button click, redirect users to registration page instead of another page
  82. Members-only page, but accessible via sharable link
  83. How can I show login popup when user clicks on download button
  84. When the user entered an unauthorized url redirect to login page
  85. How to show private pages based on a user’s role?
  86. Create password protected page, no registration
  87. Cant visualize protected password portfolio elements
  88. Why does WordPress use cookies for /wp-admin and /wp-content/plugins for non-admin users [duplicate]
  89. using wordpress login details for other website / application / forum?
  90. wp_signon returns user, in popup window, but the user is not logged in
  91. How to Create Custom Dashboard for my Laundry Website?
  92. wp_login_form() ignoring login_form action hook
  93. User content database [closed]
  94. Auto-login from backend
  95. AJAX login without a plugin does not work. when add a action to function.php
  96. Plugins effecting layout & login
  97. Redirect default login page to a custom page [duplicate]
  98. wp-admin will not redirect to wp-login.php
  99. Adding google authenticator and use only email address of user
  100. Adding a Filter to Sidbar Login Plugin to Change Login Button Lable
Categories plugins Tags login, password, plugins
Fetch commens from a specific post
Default Gutenberg CSS on frontend

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