5

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

 

12

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.

  • Amazing @Roberthue...UPPERCASE is an issue. I would have never figured that one out. – tammy Dec 21 '14 at 19:40
  • Sometimes it is. That's why usually avoid uppercase and combination of dashes -, like you used in shortcode name sme_user-email. You used one hyphen and one underscore. Keep it uniform, and you will face less issues. – Robert hue Dec 21 '14 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.