0

I need to implement a functionality where admin will be able to create posts, and for each of them generate login and password for the client. The only way to reach them from the front end will be by "/client" page (same for everybody). Depending on what login/password client will put, he will go to his unique post. So the login could be kind of page id.

I have no idea how to do it, I've seen some password protection plugins, but nothing like this. I would highly appreciate if you recommend some solution (as easy as possible). No additional features needed, no registration, no password recovery.

Thank you very much!

2
  • Would the logins be unique per post? Meaning if you create "Post A" and generate "Login A", then "Post B", would the client still use "Login A" or would there be a new "Login B" for the same client? Also, are there multiple logins per post (Client A and Client B need access to the same Post C) or is it a 1 to 1 relationship between clients and their posts?
    – Tom
    Nov 29, 2019 at 16:22
  • Thank you for your answer. No, it's as simple as it can be: Client A - login A(client email)/password - post A Client B - login B(client email)/password - post B
    – KacperG90
    Nov 29, 2019 at 16:37

1 Answer 1

0

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);
        }

    }
}

5
  • Thank you Tom, I am impressed and grateful for your work and will test it soon. However, how do I login as a client? The thing is, the login form has to be on mysite.com/client page which is available from the main menu on site...
    – KacperG90
    Nov 29, 2019 at 18:38
  • You're very welcome - I hope my code helps! Regarding login, I would give a front-end login plugin a try. There are shortcodes provided in this one (wordpress.org/plugins/wp-front-end-login-and-register) that you can embed on the Client page.
    – Tom
    Nov 29, 2019 at 18:40
  • Hello again. The code works almost perfect. I have implemented everything, including front end, using your suggested plugin, thank you! It works like charm with one user and one post, however, I wanted to test it with more users and posts and there is a problem. I can not change Client Post Association. Private posts appear in the dropdown, but when I pick one and click "save", it always come back to the default one after refresh. Any idea what can cause this problem? I guess it must be fixed within your little plugin code...
    – KacperG90
    Nov 30, 2019 at 19:50
  • It seems like there is a problem with this line:
    – KacperG90
    Nov 30, 2019 at 21:59
  • It looks like your last comment didn't include the line. Please send that when you can. I would also try to remove if(!current_user_can('administrator')){ echo sprintf('<input type="hidden" name="client_post" id="client_post" value="%s" />', $chosen_post_id); return; } and see if that helps.
    – Tom
    Nov 30, 2019 at 23:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.