I have two user forms: 1) to create a "subscriber" user, 2) to create a "pending approval" user.
I want to assign a portfolio post to every user when they register. The name of the portfolio post should be firstname_last_name.
To make that happen, I have added an action to update the user on registration such that the nickname has the above format.
function set_default_display_name( $user_id ) {
$user = get_userdata( $user_id );
$name = sprintf( '%s %s', $user->first_name, $user->last_name );
$args = array(
'ID' => $user_id,
'display_name' => $name,
'nickname' => $name
);
wp_update_user( $args );
}
add_action( 'user_register', 'set_default_display_name',10, 1 );
Then I am creating the custom post with post type portfolio using the same hook again but with a lower priority so that it is executed after the user nickname is updated.
add_action( 'user_register', 'create_portfolio_post', 20, 1 );
function create_portfolio_post( $user_id )
{
// Get user info
$user_info = array();
$user_info = get_userdata( $user_id );
$portfolio_owner_email = $user_info->user_email;
// 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
if ($_POST["log_time"] = "true") {
$new_post_id = wp_insert_post( $user_post );
update_post_meta( $new_post_id, 'wpcf-access-to-skills-overview-section', 2 );
update_post_meta( $new_post_id, 'wpcf-access-to-skills-growth-section', 2 );
update_post_meta( $new_post_id, 'wpcf-access-to-skills-range-section', 2 );
update_post_meta( $new_post_id, 'wpcf-access-to-projects-section', 2 );
update_post_meta( $new_post_id, 'wpcf-access-to-endorsement-section', 2 );
update_post_meta( $new_post_id, 'wpcf-portfolio-owner-email', $portfolio_owner_email );
}
}
?>
The portfolio post is created just fine BUT the user nickname is changed to firstname_lastname only for the subscriber form. For the pending user' form, the system uses the user email address to create the portfolio post type.
The code does not differentiate between subscriber or pending user type so why is the user email used to create a post for the pending user? Is there any setting I am missing on the user form?