skip to Main Content

I have a shortcode showing the username of the current user, it works. What I want to do is insert conditions.

if the user has the username show it, otherwise show first name or something else.

I searched on google and here on stack, I understood this is possible thanks to the if and else conditions but I’ve never done it, can someone help me understand how I can insert these conditions in my shortcode?

also I would like to show other information as well, such as e-mail, last name, registration date and the like, is there a documentation / list on where to get all these values?

function display_current_username () {
    $user = wp_get_current_user();
    $name = $user->display_name;
    return $user->display_name;
}
add_shortcode('display_name', 'display_current_username');

2

Answers


  1. If/Else conditionals are a foundational aspect of PHP. It doesn’t matter really if it’s WP or not, they work the same.

    To answer the main question, you would add the conditional like this:

    function display_current_username () {
    
        /* ADDED BASED ON WPEXPERTS ANSWER
           This is a really good check to do to make sure that WP doesn't
           throw an error if wp_get_current_user returns FALSE/0
        */
        if( !is_user_logged_in() ) :
            return 'Something if user isn't logged in';
        endif;
    
        $user = wp_get_current_user();
        $name = $user->display_name;
        
        // If the $name var is empty, return the user_firstname property.
        if ( $name === '' ) :
            return $user->user_firstname;
        endif;
        
        return $name;
    }
    add_shortcode('display_name', 'display_current_username');
    

    All the documentation for the user data object returnd using wp_get_current_user() can be found here: https://developer.wordpress.org/reference/functions/wp_get_current_user/#comment-437

    To get more user data, use the get_userdata() function

    You can pass the user id to get all the data:

    $user_data = get_userdata( $user->ID );
    

    You can use that in your shortcode as well. Something like this:

    function display_current_username () {
        $user = wp_get_current_user();
        $name = $user->display_name;
        $user_data = get_userdata( $user->ID );
    
        /*
        Do something with the $user_data information
        for example:
        $user_registration_date = $user_data->user_registered;
        */
    
        
        // If the $name var is empty, return the user_firstname property.
        if ( $name === '' ) :
            return $user->user_firstname;
        endif;
        // This is super basic and is only an example of what you can do.
        return 'User name: ' . $name . "nr" . 'User registered: ' . $user_registration_date;
    }
    add_shortcode('display_name', 'display_current_username');
    

    get_userdata is a wrapper/helper for get_user_by(). Here is the full object returned by get_userdata(): https://developer.wordpress.org/reference/functions/get_user_by/#comment-495

    EDIT

    Based on question in comment

    function display_current_username () {
        $user = wp_get_current_user();
        $display_name = $user->display_name;
        $first_name = $user->user_firstname;
        $name = 'Set Custom Text Here';
        
        // If display name isn't empty;
        if ( $display_name !== '' ) :
            return $display_name;
        endif;
    
        // If first name isn't empty;
        if ( $first_name !== '' ) :
            return $first_name;
        endif;
    
         /* THIS COULD ALSO BE USED
         // This just assigns the user properties to the $name var
         // instead of returning.
         if ( $display_name !== '' ) :
            $name = $display_name;
         elseif ( $first_name !== '' ) :
            $name = $first_name;
         endif;
         */
        
        // If it gets to this point, return the custom text.
        return $name;
    }
    add_shortcode('display_name', 'display_current_username');
    
    Login or Signup to reply.
  2. Try this:

    add_shortcode('display_name', 'display_current_username');
    function display_current_username () {
        if( !is_user_logged_in() ){
            return '';
        }
        $user_id = get_current_user_id();
        $user_data = get_userdata( $user_id );
        $first_name = get_user_meta( $user_id, 'first_name', 1 );
        if( !empty( $first_name ) ){
            return $first_name;
        } elseif( !empty($user_data->user_nicename) ){
            return $user_data->user_nicename;
        } else {
            return $user_data->display_name;
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search