Skip Navigation

[Closed] Inconsistent behavior across two user forms

This support ticket is created 3 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 10 replies, has 3 voices.

Last updated by Christian Cox 3 years ago.

Assisted by: Christian Cox.

Author
Posts
#2000017

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?

#2000225

Hello,

How do you setup the register form?
Please provide detail steps to reproduce the same problem, I need to test and debug it in my localhost, thanks

#2000237

Do you mind moving the ticket to US time?

#2000239

Done

#2000589

Hello, this could be a problem:

if ($_POST["log_time"] = "true") { 

This is a common mistake - you're assigning the value instead of testing it. The correct way to test is with "==":

if ($_POST["log_time"] == "true") { 
#2000989

Chris,

Thanks for the input. I fixed that part but the problem is even before that when I am trying to update the nickname.
It seems like the first and last names are not retrieved. I have tried to use wpv-user shortcode too but when I look at the nickname I just get "-" instead of "first_name-last_name".

function set_default_display_name_prowess( $user_id ) {
  $user_info = get_userdata( $user_id );
  //$first_name = $user_info->first_name; do_shortcode('[wpv-user field="user_firstname"]
  $first_name = do_shortcode('[wpv-user field="user_firstname"]');
//  $last_name = $user_info->last_name;
	$last_name = do_shortcode('[wpv-user field="user_lastname"]');
  $full_name = "";
  $full_name .= $first_name;
  $full_name .= "-";
  $full_name .= $last_name;
//  $name = sprintf( '%s %s', $user->first_name, $user->last_name );
  $args = array(
    'ID'           => $user_id,
    'display_name' => $full_name,
    'nickname'     => $full_name
  );
  wp_update_user( $args );
}

add_action( 'user_register', 'set_default_display_name_prowess',10, 1 );
#2001095

I would supply the User ID explicitly to the wpv-user shortcode, and try logging everything:

function set_default_display_name_prowess( $user_id ) {
  error_log('user id: ' . $user_id);
  $user_info = get_userdata( $user_id );
  error_log('userinfo: ' . print_r($user_info, true));
  $first_name = do_shortcode('[wpv-user field="user_firstname" id="' . $user_id . '"]');
  $last_name = do_shortcode('[wpv-user field="user_lastname" id="' . $user_id . '"]');
  error_log('first_name: ' . $first_name);
  error_log('last_name: ' . $last_name);
  $full_name = "";
  $full_name .= $first_name;
  $full_name .= "-";
  $full_name .= $last_name;
  error_log('full name: ' . $full_name);
  //  $name = sprintf( '%s %s', $user->first_name, $user->last_name );
  $args = array(
    'ID'           => $user_id,
    'display_name' => $full_name,
    'nickname'     => $full_name
  );
  wp_update_user( $args );
}
 
add_action( 'user_register', 'set_default_display_name_prowess',10, 1 );

If you're not seeing first name and last name correctly logged in userinfo, something is going on that is preventing the first and last name from being saved correctly. It could be an error in the field slugs or something else, not sure.

#2001337

Can you please enable a private message so that I can share the log with you?

#2003569

Sure, private reply fields are enabled here.

#2006499

Okay so based on the logs is looks like no first or last name information is found for User 191775597. If you edit the User 191775597, do you see first and last names in their User profile? These are not required fields, so it's possible this User just does not have that information in their profile.
/wp-admin/users.php?id=191775597
Can you provide a screenshot of the first and last name fields when you edit this User?

All those PHP warnings and the fatal error in your custom code related to the toolset_get_related_post function should be resolved!

#2006567

Chris,

Strange as it seems that user id does not exist in the system. I am confused why that happened and why would this number even show if the user does not exist.

What could I be missing?

The topic ‘[Closed] Inconsistent behavior across two user forms’ is closed to new replies.