如何为每个新注册的用户在一个会员网站的样本页面

时间:2018-02-07 作者:Deb

我有一个会员网站,我的用户可以在那里创建自己的页面。我有一些页面,我希望每个新用户都有这些页面(将我的页面复制为他们自己的页面)。

对于每一个新用户,我都必须指导他们如何复制我的页面&;发布它们。是他们的任何方式或插件或代码,每当有人在我的网站注册,他将有一些预先发布的页面。

这就像新的WordPress注册。当有人第一次安装WordPress时,他总是有一个示例页面。这是他们为新用户提供此类页面样本或副本的任何方式。

我们将非常感谢您的帮助。

1 个回复
SO网友:WebElaine

有个钩子叫user_register() 你可以用的。它发生在创建新用户的过程中-就在他们被添加到数据库并具有ID之后,但在保存类似usermeta的first\\u名称之前。然后您可以使用wp_insert_post() 添加新帖子并将其指定为“作者”

<?php
// When a user first registers, run our function
add_action(\'user_register\', \'wpse_293428_create_user_post\', 10, 1);
function wpse_293428_create_user_post($user_id) {
    // Set up a simple post array
    $sample_post = array(
        // Set to desired post type: post, page, etc.
        \'post_type\' => \'post\',
        \'post_title\' => \'Sample Post\',
        // Post content can get much more complex if you need html etc.
        \'post_content\' => \'This is a test post\',
        // Choose post status you want: could be a draft, or could be published
        \'post_status\' => \'publish\',
        // Use the User ID that WP just created for this user
        \'post_author\' => $user_id,
        // Only if it\'s a Post, set Category - requires an array
        \'post_category\' => array(1)
    );
    wp_insert_post($sample_post);
}
?>
您可能需要更改新帖子出现的类别,或者如果切换到创建页面,请确保删除post_category. 根据您的需要,填写更复杂的post_content 可能会有点棘手,所以从简单开始,然后慢慢构建出你想要的东西。

结束

相关推荐

Get users that likes the post

我有一个类似ajax的帖子系统,当用户喜欢帖子时,系统会在帖子中添加一个帖子元,并将其命名为(post\\u liked\\u users)。post\\u likes\\u users元值示例:a:1:{s:6:\"user-1\";i:1;}现在,如何才能获得喜欢该帖子的用户?注意:我不能像杰克·约翰逊那样只使用foreach答案,我必须使用WP\\u User\\u Query或WP\\u Query,因为我要使用offest和number。