How to have sample page for each new register users in a membership website
I have a membership website where my users can create their own page. I have some pages which I want every new users to have them (duplicate my page as their own).
For every new users I have to instruct them about how to copy past my page & publish them. Is their any way or plugin or codes by which whenever some one register in my website he will have some pre-published page.
It is like new WordPress registration. When someone install WordPress 1st time, he always have 1 sample page. Like this is their any way to have such sample or copy of a page for new users.
Any helps will be highly appreciated.
Solutions
There's a hook called
user_register()
that you could use. It happens during the creation of the new user - right after they are added to the database and have an ID, but before the usermeta like first_name has been saved. You can then use
wp_insert_post()
to add the new post and assign them as the "author."
<?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);
}
?>
You may want to change what category(ies) the new
post appears
in, or if you switch to creating a Page make sure to remove
post_category
. Depending on your needs, filling in more complex
post_content
may get a bit tricky so start simple and then slowly build out what you want.