Skip to content

Snippets / PHP

PHP

Auto Create Collection on Account Creation

D Donald McGuinn · 327 views · Updated 2 years ago
This snippet will create a collection for the user when they make an account. It will be named "USERNAME's COLLECTION"
function create_post_for_new_user($user_id){
$user = get_userdata($user_id);
$username = $user->user_login;
 
// Post data
$post_data = array(
'post_author' => $user_id,
'post_title' => $username . "'s collection",
'post_status' => 'publish',
'post_type' => 'collection'
);
 
// Create the new post
wp_insert_post($post_data);
}
 
// Hook into 'user_register'
add_action('user_register', 'create_post_for_new_user');
PHP · 18 lines

Discussion 1

Ask a question or share how you used this.

Log in to join the discussion.
gabrielvb 2 years ago

Hello, it gives an error : syntax error, unexpected variable "$post_data"

Donald McGuinn 2 years ago

This code was given by Arian. I would message a ticket in and see if they can give you an updated code.

smirceamihai 1 year ago

This code worked for me: function create_favorites_for_new_user($user_id){ $user = get_userdata($user_id); $username = $user->user_login; $post_data = array( 'post_title' => 'Mine favoritter', 'post_status' => 'publish', 'post_author' => $user_id, 'post_type' => 'collection', ); wp_insert_post($post_data); } add_action('user_register', 'create_favorites_for_new_user');

smirceamihai 1 year ago

This is the code generated by ChatGPT if asked :)

Voxel Guide assistant
Ask about snippets, addons & how-tos

Hey — how can I help?

I can dig through the library for snippets, addons, and tutorials.

AI can be wrong — double-check anything important.