自动在WordPress网站上为每个用户创建帖子

发布于 2025-01-24 13:22:12 字数 1415 浏览 2 评论 0原文

我正在尝试在注册新用户到我的网站时自动创建自定义帖子(用户)。我只是与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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难如初 2025-01-31 13:22:12

您的挂钩不正确,请使用 user_register> user_register 操作挂钩在用户注册和用户已注册和注册后触发的动作挂钩通过$ user_id作为变量:

add_action('user_register', 'create_authors_page');
function create_authors_page( $user_id ) {
    // do your stuff
}

您还可以使用 a>挂钩每次用户更新配置文件触发。

Your hook is incorrect, use user_register action hook which fires after a user has registered and passes $user_id as a variable:

add_action('user_register', 'create_authors_page');
function create_authors_page( $user_id ) {
    // do your stuff
}

You can also use profile_update hook that trigger each time user update profile.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文