How do I get the user firstname in wordpress?

Here’s my code in wordpress functions.php. I want to display user information in gravity form fields The top function works but the bottom doesn’t and I can’t figure out what to use to display first name.

add_filter('gform_field_value_pm_id', 'populate_pm_id');
function populate_pm_id($value){
    return $user_ID = get_current_user_id(); 
    echo $user_ID = get_current_user_id(); 

}

add_filter('gform_field_value_pm_firstname', 'populate_pm_firstname');
function populate_pm_firstname($value){
    return $user_info->first_name = get current_user_first_name(); 
    echo $user_info->first_name = get_current_user_first_name();

}

Related posts

Leave a Reply

2 comments

  1. Try this code to get user first name and last name.

    <?php 
          $user_info = get_userdata(get_current_user_id());
          $first_name = $user_info->first_name;
          $last_name = $user_info->last_name;
          echo "$first_name $last_name";
    ?>
    
  2. yo can get the user and its data via

    $current_user = wp_get_current_user();
    

    to get the first name from that object, you need to

    $first_name = $current_user->user_firstname;
    

    you will still get an user object, even if you are not logged in.
    here is a var_dump on a non-logged-in user:

    object(WP_User)#1265 (8) {
      ["data"]=> object(stdClass)#1267 (0) {}
      ["ID"]=> int(0)
      ["caps"]=> array(0) {}
      ["cap_key"]=> NULL
      ["roles"]=> array(0) {}
      ["allcaps"]=> array(0) {}
      ["filter"]=> NULL
      ["site_id":"WP_User":private]=> int(0)
    }
    

    more infos and details here.