I am struggling to create a custom post in Wordpress using register_new_user hook. The code seems to be fine but nothing happens. I have even run a debug log but there is no data regarding the code. What could I be missing?
I ahev also referenced the other post: Creating a custom post type upon registration But still nothing works even if I select user_register hook or register_new_user hook.
The code:
add_action( 'register_new_user', 'create_portfolio_post', 10, 1 );
function create_portfolio_post( $user_id )
{
error_log('create_portfolio_post called with user ID: ' . $user_id);
// Get user info
$user_info = array();
$user_info = get_userdata( $user_id );
// $user_roles = $user_info->roles;
// New code added
// $this_user_role = implode(', ', $user_roles );
//if ($this_user_role == 'subscriber')
//{
// Create a new post
$user_post = array(
'post_title' => $user_info->nickname,
'post_author' => $user_id,
'post_content' => $user_info->nickname,
'post_status' => 'publish', // <- here is to publish
'post_type' => 'portfolio' // <- change to your cpt
);
// Insert the post into the database
wp_insert_post( $user_post );
error_log('wp_insert_post called with arguments: ');
error_log(print_r($user_post, true));
}