Skip to content

Instantly share code, notes, and snippets.

@champsupertramp
Last active December 8, 2024 04:32
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' );
@unbroken-untamed
Copy link

unbroken-untamed commented Dec 8, 2024

@champsupertramp

I inserted the code snippet in my functions.php file as you can see here -- https://prnt.sc/7PmdDPBzGpnY

Then I added the shortcode [um_user user_id="" meta_key="first_name" ] and [um_user user_id="" meta_key="user_email" ] on a page, as you can see here--- https://prnt.sc/qbI-fSS9MND0

Unfortunately, no user information is seen on the frontend, as you can see here --- https://prnt.sc/EKoSztzFyirl

Please, what is wrong?

Is the code still working? --or is there an updated version of it?

How do I fix this issue?

Special Regards.

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