Skip to content

Instantly share code, notes, and snippets.

@champsupertramp
Last active August 31, 2023 09:06
Show Gist options
  • Save champsupertramp/a1ed201cb05ff68bcecac4c7cd5b004b to your computer and use it in GitHub Desktop.
Save champsupertramp/a1ed201cb05ff68bcecac4c7cd5b004b to your computer and use it in GitHub Desktop.
Ultimate Member - User meta shortcodes
/**
* Returns a user meta value
* Usage [um_user user_id="" meta_key="" ] // leave user_id empty if you want to retrive the current user's meta value.
* meta_key is the field name that you've set in the UM form builder
* You can modify the return meta_value with filter hook 'um_user_shortcode_filter__{$meta_key}'
*/
function um_user_shortcode( $atts ) {
$atts = extract( shortcode_atts( array(
'user_id' => get_current_user_id(),
'meta_key' => '',
), $atts ) );
if ( empty( $meta_key ) ) return;
if( empty( $user_id ) ) $user_id = get_current_user_id();
$meta_value = get_user_meta( $user_id, $meta_key, true );
if( is_serialized( $meta_value ) ){
$meta_value = unserialize( $meta_value );
}
if( is_array( $meta_value ) ){
$meta_value = implode(",",$meta_value );
}
return apply_filters("um_user_shortcode_filter__{$meta_key}", $meta_value );
}
add_shortcode( 'um_user', 'um_user_shortcode' );
@champsupertramp
Copy link
Author

Hi @Kymillionare

You can use this plugin to add the code snippets:
https://wordpress.org/plugins/code-snippets/

Regards,

@akwilson77
Copy link

Hi,
i'm trying to use your snippet and it works for first_name / last_name but not for user_email.

Can you help me with that ?

@JohnDeeeee
Copy link

Hi, thanks for the great code. It works perfectly. I have only a small problem when listing user roles. If there are more roles, a comma is made between them without a space (Admin,Moderator,Editor, ...). How do I add a space or remove a comma? Thank you.

@jalecs
Copy link

jalecs commented Aug 19, 2021

Hello, is it possible to limit the user's registration form? also, I'm trying to display the {total_users} in some of my pages but now working

@JenniferMattheson
Copy link

JenniferMattheson commented Aug 31, 2021

Is there a way to pull the user's email address and insert it into html?
I can get the user's email address to display using your code snippet and "[um_user meta_key="user_email"]"

But I would like that content to behave as a link: <a href="mailto:__________________">Send Email</a>

@jainanda
Copy link

jainanda commented Sep 6, 2021

Hi everyone
Using um_profile_id() to get the current UM user ID seems to make more sense instead of get_current_user_id()

@Darklirah
Copy link

Darklirah commented Mar 16, 2022

@ALL
How can I use the received Profile Photo URL to dispolay the photo?
To use somethng like this
<img src= [um_user user_id="19" meta_key="profile_photo" ]>
Does not work?
Ty for help

Got it just used this
<img src= '[um_user user_id="19" meta_key="profile_photo" ]'>
instead of this
<img src= [um_user user_id="19" meta_key="profile_photo" ]>

the ' were the solution

@Darklirah
Copy link

Darklirah commented Mar 16, 2022

Another Question
the meta user_login to display the Username does not work?
Ty für helping me

@aknahin
Copy link

aknahin commented Dec 10, 2022

Hi, I am using this code in code snippet:

/**
 * Returns a user meta value
 * Usage [um_user user_id="" meta_key="" ] // leave user_id empty if you want to retrive the current user's meta value.
 * meta_key is the field name that you've set in the UM form builder
 * You can modify the return meta_value with filter hook 'um_user_shortcode_filter__{$meta_key}'
 */
add_action('template_redirect','sc_init_um_user_shortcode');
add_action('init','sc_init_um_user_shortcode');
function sc_init_um_user_shortcode(){
	add_shortcode( 'um_user', 'sc_um_user_shortcode' );
}

function sc_um_user_shortcode( $atts ) {
	$atts = shortcode_atts( array(
		'user_id' => get_current_user_id(),
		'meta_key' => ''
	), $atts );
	extract( $atts );
	if ( ! $user_id || empty( $meta_key ) ) return;
    
    $raw_meta_value  = get_user_meta( $user_id, $meta_key, true );
    
    if( is_serialized( $raw_meta_value ) ){
       $meta_value = unserialize( $raw_meta_value );
    }else if( is_array( $raw_meta_value ) ){
         $meta_value = implode(",",$raw_meta_value);
    }else{
    	$meta_value = $raw_meta_value;
    } 

    return apply_filters("um_user_shortcode_filter__{$meta_key}", $meta_value,  $raw_meta_value, $user_id );
}
add_filter("um_user_shortcode_filter__profile_photo","um_user_shortcode_filter__profile_photo", 10, 3);
function um_user_shortcode_filter__profile_photo( $meta_value,  $raw_meta_value, $user_id ){
    
   return  UM()->uploader()->get_upload_user_base_url( $user_id )."/".$meta_value; 
}

and this short code:
[um_user meta_key="image_realtor"]
as I have create an image upload field with "image_realtor" meta. But it is showing a value like this:
stream_photo_36318a1a_76850750071ddc5c51a9c62d3eca15a26145531a.png
No image is showing. It is working for other fields like number or text field.

@aknahin
Copy link

aknahin commented Dec 10, 2022

This code shows an error image:
image

@champsupertramp
Copy link
Author

Hi @aknahin

Replace this:

add_filter("um_user_shortcode_filter__profile_photo","um_user_shortcode_filter__profile_photo", 10, 3);
function um_user_shortcode_filter__profile_photo( $meta_value,  $raw_meta_value, $user_id ){
    
   return  UM()->uploader()->get_upload_user_base_url( $user_id )."/".$meta_value; 
}

with the following:

add_filter("um_user_shortcode_filter__image_realtor","um_user_shortcode_filter__image_realtor", 10, 3);
function um_user_shortcode_filter_image_realtor( $meta_value,  $raw_meta_value, $user_id ){
    
   return  UM()->uploader()->get_upload_user_base_url( $user_id )."/".$meta_value; 
}

@aknahin
Copy link

aknahin commented Dec 11, 2022

Thanks for your reply. But unfortunately, that hasn't worked. even after changing this code there is nothing.

@PeregrinoHTX
Copy link

I had this working fine, and then my theme updated. I have gone back and put the code in the header, and the short code is still there, but I cannot make it work now. It's just showing the short code. What am I forgetting?

@champsupertramp
Copy link
Author

Hi @PeregrinoHTX please check if your theme has stopped supporting shortcode in the post/page templates. How do you add the custom shortcode? Is it when you edit/add a page/post?

@PeregrinoHTX
Copy link

PeregrinoHTX commented Feb 15, 2023 via email

@workboy101
Copy link

hello guys, does this code still work? i have copy and pasted but i cant seem to get it to work.

@champsupertramp
Copy link
Author

Hi @workboy101 - this still works. Could you please share your shortcode?

@kinglin
Copy link

kinglin commented Mar 26, 2023

hello @champsupertramp , the code works for "first_name", but not for "user_login" or "user_email", may I know how to solve it? thanks. [um_user meta_key="user_email"]

@champsupertramp
Copy link
Author

Hi @kinglin this code is for the data coming from the wp_usermeta table. The user_login and user_email are coming from the wp_users table. So this shortcode won't work with those meta keys.

@kinglin
Copy link

kinglin commented Mar 27, 2023

thanks @champsupertramp , is there any way I can query wp_usermeta table?

@workboy101
Copy link

workboy101 commented Mar 27, 2023

i used this code

/**

  • Returns a user meta value

  • Usage [um_user user_id="" meta_key="" ] // leave user_id empty if you want to retrive the current user's meta value.

  • meta_key is the field name that you've set in the UM form builder

  • You can modify the return meta_value with filter hook 'um_user_shortcode_filter__{$meta_key}'
    */
    function um_user_shortcode( $atts ) {
    $atts = extract( shortcode_atts( array(
    'user_id' => get_current_user_id(),
    'meta_key' => '',
    ), $atts ) );

    if ( empty( $meta_key ) ) return;

    if( empty( $user_id ) ) $user_id = get_current_user_id();

    $meta_value = get_user_meta( $user_id, $meta_key, true );
    if( is_serialized( $meta_value ) ){
    $meta_value = unserialize( $meta_value );
    }
    if( is_array( $meta_value ) ){
    $meta_value = implode(",",$meta_value );
    }
    return apply_filters("um_user_shortcode_filter__{$meta_key}", $meta_value );

}
add_shortcode( 'um_user', 'um_user_shortcode' );

perhaps i am setting it up wrong, pls help.
Been on this for almost 2 weeks now. Nothing happens when i use the code

@sambalbetawi
Copy link

hi @champsupertramp thanks for the code. I copied your latest one 2020 version into my functions.php child theme
but, it doesn't return any value when i use the shortcode [um_user user_id="" meta_key="first_name" ]

am i doing something wrong?

@champsupertramp
Copy link
Author

Hi @sambalbetawi Try removing the user_id="", just use [um_user meta_key="first_name" ]

@Cbookwap
Copy link

Cbookwap commented Aug 24, 2023

Hello @champsupertramp,

Kindly save me from this much stress. I will never stop appreciating you, if you can walk me step by step, on how to achieve this below task. I have watch almost all YouTube videos, none of them specifically take on Redirect after registration. They focus much on redirect after login.

This is the task, I want to achieve.

I am using Ultimate Member, to register my users. So far it is great! Now, I want my users to be redirected to a custom page, which I have set, in the User Role set for the FORM.

What am I saying?

The Ultimate Member form, I'm talking about, has Subscriber Role set in its settings. Then, I edited Subscriber role, and set New registration to be redirected to a custom page. I type in the page url, which is a page on my website. (note, the page is typed in full. E. G https://example.com/verification)

Unfortunately, this is not working. Once user register, it just redirect to home page. This is awkward! User don't get to know, if their registration is successful or not.

But, if I pick the custom message, in the User Role settings, and write some custom message. The message will display.

Why is it acting this way? What have I done wrong???

I'm urgently in need of your reply, as my eLearn website is about to be launched. TutorLMS form is not satisfactorily, this is why I'm using Ultimate Member form.

Kindly, help. I wait here pls.

Thanks.

@Cbookwap
Copy link

Waiting @champsupertramp

Please....

@Bilgehano
Copy link

Bilgehano commented Aug 30, 2023

Hello,
Can we somehow achive to retrieveng a field that contains the pdf file ? The code works perfectly fine until the field with a pdf file and i need it display . If anyone can help me out i would appreciate it .

@champsupertramp
Copy link
Author

Hello @champsupertramp,

Kindly save me from this much stress. I will never stop appreciating you, if you can walk me step by step, on how to achieve this below task. I have watch almost all YouTube videos, none of them specifically take on Redirect after registration. They focus much on redirect after login.

This is the task, I want to achieve.

I am using Ultimate Member, to register my users. So far it is great! Now, I want my users to be redirected to a custom page, which I have set, in the User Role set for the FORM.

What am I saying?

The Ultimate Member form, I'm talking about, has Subscriber Role set in its settings. Then, I edited Subscriber role, and set New registration to be redirected to a custom page. I type in the page url, which is a page on my website. (note, the page is typed in full. E. G https://example.com/verification)

Unfortunately, this is not working. Once user register, it just redirect to home page. This is awkward! User don't get to know, if their registration is successful or not.

But, if I pick the custom message, in the User Role settings, and write some custom message. The message will display.

Why is it acting this way? What have I done wrong???

I'm urgently in need of your reply, as my eLearn website is about to be launched. TutorLMS form is not satisfactorily, this is why I'm using Ultimate Member form.

Kindly, help. I wait here pls.

Thanks.

Hi @peculiardigitals Sorry for the late response. How many roles are assigned to the Subscriber? If there's more than 1 role, you need to make the Subscriber role the Primary role by adding a Priority number in the role settings. This means by adding a priority number, the register form will use this role's settings for redirection and others.

@Cbookwap
Copy link

I will try this your suggestion and revert. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment