How to create a shortcode with 1 parameter (atts)

I am having trouble getting a shortcode to work with 1 attribute.

Here is my shortcode [sme_user-email userID="2"]

Here are my scripts – none of them work.

function getUserEmail_func($atts) {
    extract(shortcode_atts(array('userID' => 1,), $atts));
    $user_info = get_userdata($atts);
    return $user_info->user_email;
}
add_shortcode('sme_user-email', 'getUserEmail_func');

.

function getUserEmail_func($atts) {
    $user_info = get_userdata($atts);
    return $user_info->user_email;
}
add_shortcode('sme_user-email', 'getUserEmail_func');

.

function getUserEmail_func($atts) {
    $user_info = get_userdata($atts['userID]');
    return $user_info->user_email;
}
add_shortcode('sme_user-email', 'getUserEmail_func');

This one works — but I do not want to hardcode the user ID. I was hoping to be able to do it dynamically

function getUserEmail_func($atts) {
    $user_info = get_userdata(2);
    return $user_info->user_email;
}
add_shortcode('sme_user-email', 'getUserEmail_func');

 

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Here is how you should create shortcode.

First you will have to define $atts item in get_userdata because $atts is an array. Also I think there is also some issues with uppercase attributes names, so you should use attribute in lower case. So instead of userID, use userid.

function getUserEmail_func( $atts ) {
    $user_info = get_userdata( $atts['userid'] );
    return $user_info->user_email;
}
add_shortcode( 'sme_user-email', 'getUserEmail_func' );

Have tested it and it’s working.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x