0

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.

  • hmmm breaking this down, I can see there are 2 separate questions, "How do I create a post/page?" and "How do I run code when somebody registers?" – Tom J Nowell Feb 7 '18 at 17:40
  • Thank for comment. What I really want is when someone register in my website as a member, he will have 1-2 already published page (duplicate of some of my pages). So that he do not have to create new page by himself. I want to make it simple for them. So my question was is their any plugin or function.php snippets codes to make it easily. – Deb Feb 7 '18 at 17:46
  • So you need to create those pages automatically in code – Tom J Nowell Feb 7 '18 at 17:50
  • @TomJNowell Yes, is there any such codes. – Deb Feb 7 '18 at 18:02
2

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.

  • I tried, but it not worked. There was not any pre-published page. Anyway thanks for helping. – Deb Feb 7 '18 at 18:01
  • 1
    @Deb the code example above creates a post not a page, and demonstrates how you would create a post of type post on registration. Changing it to create a post of type page is rather trivial/ As I said elsewhere, answers will tell you how to do something, so that you understand the solution, they aren't copy paste fully baked solutions. – Tom J Nowell Feb 7 '18 at 18:06
  • @TomJNowell So from above code, if I replace the 'post' with 'page', will it work. – Deb Feb 7 '18 at 18:12
  • Deb, this site is set up to provide ideas but often using those ideas involves manipulating the code yourself. As I already mentioned above, if you want to create a Page instead of a Post, you change the post type, and also remove the post_category line since categories do not apply to Pages. I would suggest trying the original code as-is first, and seeing if it does indeed create a Post. If so, then you can modify bit by bit until you reach your desired end goal. – WebElaine Feb 7 '18 at 22:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.