自动在WordPress网站上为每个用户创建帖子
我正在尝试在注册新用户到我的网站时自动创建自定义帖子(用户
)。我只是与php相关的几乎不熟悉,但是我一直在从Stackoverflow的另一个问题中工作:使用WP_Insert_post自动为每个用户创建一个帖子
我理想地喜欢创建一个页面,该页面在注册或更新用户信息时在自定义字段上携带。我已经使用ACF创建了与之关联的自定义字段;用户(在代码中列出为小写变量)和待办事项的自定义帖子(在代码中列出为大写变量)。
function create_authors_page( $user_id ) {
$the_user = get_userdata( $user_id );
$new_user_name = $the_user->user_login;
$PostSlug = $user_id;
$PostGuid = home_url() . "/" . $PostSlug;
$member_bio = get_field('member_bio');
$contact_info = get_field('contact_info');
$member_affiliation = get_field('member_affiliation');
$my_post = array( 'post_title' => $new_user_name,
'post_type' => 'users',
'post_content' => '',
'post_status' => 'publish',
'post_theme' => 'user-profile',
'guid' => $PostGuid );
$NewPostID = wp_insert_post( $my_post );
$Member_Bio = $member_bio;
$Contact_Info = $contact_info;
$Member_Affiliation = $member_affiliation;
update_post_meta( $NewPostID, $Member_Bio, $Contact_Info, $Member_Affiliation );
return $NewPostID;
}
add_action('Publish_members','create_authors_page');
I'm trying to automatically create a custom post (users
) upon registering a new user to my site. I'm just barely familar with php, but I've been working from another question from stackoverflow: Automatically create a post for each user using wp_insert_post
I would ideally love to create a page that carries over custom fields upon registering or updating user information. I've used ACF to create custom fields that are associated with; users (listed in the code as lowercase variables), and the to-be-created custom posts (listed in the code as uppercase variables).
function create_authors_page( $user_id ) {
$the_user = get_userdata( $user_id );
$new_user_name = $the_user->user_login;
$PostSlug = $user_id;
$PostGuid = home_url() . "/" . $PostSlug;
$member_bio = get_field('member_bio');
$contact_info = get_field('contact_info');
$member_affiliation = get_field('member_affiliation');
$my_post = array( 'post_title' => $new_user_name,
'post_type' => 'users',
'post_content' => '',
'post_status' => 'publish',
'post_theme' => 'user-profile',
'guid' => $PostGuid );
$NewPostID = wp_insert_post( $my_post );
$Member_Bio = $member_bio;
$Contact_Info = $contact_info;
$Member_Affiliation = $member_affiliation;
update_post_meta( $NewPostID, $Member_Bio, $Contact_Info, $Member_Affiliation );
return $NewPostID;
}
add_action('publish_members', 'create_authors_page');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的挂钩不正确,请使用 user_register> user_register 操作挂钩在用户注册和用户已注册和注册后触发的动作挂钩通过
$ user_id
作为变量:您还可以使用 a>挂钩每次用户更新配置文件触发。
Your hook is incorrect, use user_register action hook which fires after a user has registered and passes
$user_id
as a variable:You can also use profile_update hook that trigger each time user update profile.