Skip to content
Advertisement

How to add if and else condition in wordpress shortcode

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');

Advertisement

Answer

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');
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement