受登录/密码保护的“客户端页面”

时间:2019-11-29 作者:KacperG90

我需要实现一个功能,管理员将能够创建帖子,并为每个帖子生成客户端的登录名和密码。从前端联系他们的唯一方法是通过“/客户端”页面(每个人都一样)。根据客户端的登录名/密码,他将转到自己独特的帖子。因此,登录可以是一种页面id。

我不知道怎么做,我见过一些密码保护插件,但没有这样的。如果您能推荐一些解决方案(尽可能简单),我将不胜感激。无需其他功能,无需注册,无需密码恢复。

非常感谢!

1 个回复
SO网友:Tom

我建议你重新思考。我认为您可以使用POST生成用户登录(这很复杂,但可以做到),而不是让POST生成用户登录associate a post to a user.

不幸的是,似乎没有任何插件可以为您做到这一点。我写了这个快速插件,你可以用它来开始这还没有经过测试,因此使用时要小心,风险自负。

下面概述的插件有两个作用:

为管理员的所有用户配置文件添加一个字段,允许您将用户绑定到私人帖子。注意:这只显示私人帖子,因此所讨论的帖子必须是私人的安装步骤:

将上述内容上载到插件文件夹:/wp-content/plugins/your-custom-plugin/custom-plugin.php.作为您的客户端登录,并通过访问示例进行测试。com/客户端/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);
        }

    }
}