我建议你重新思考。我认为您可以使用POST生成用户登录(这很复杂,但可以做到),而不是让POST生成用户登录associate a post to a user.
不幸的是,似乎没有任何插件可以为您做到这一点。我写了这个快速插件,你可以用它来开始这还没有经过测试,因此使用时要小心,风险自负。
下面概述的插件有两个作用:
为管理员的所有用户配置文件添加一个字段,允许您将用户绑定到私人帖子。注意:这只显示私人帖子,因此所讨论的帖子必须是私人的如果管理员在其个人资料中设置了用户的私人帖子,则会检测到带有slug“client”的页面,并将用户重定向到其私人帖子安装步骤:
将上述内容上载到插件文件夹:/wp-content/plugins/your-custom-plugin/custom-plugin.php.登录到wp admin,激活“自定义客户端登录后”创建一个名为“客户端”的页面创建一个私人帖子(例如,“客户a的私人帖子”)为客户端创建或编辑用户登录名。滚动到他们个人资料的底部,您将看到“客户帖子关联”。选择您希望他们在访问示例时看到的帖子。com/client//li>作为您的客户端登录,并通过访问示例进行测试。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);
}
}
}