user meta data with shortcode
-
I would like to display the user meta data in my profile page which is custom template.
I added this code in theme function.php
/** * 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' );I added this shortcode in my profile page:
[um_user user_id=” ” meta_key=”country”]
[um_user user_id=” ” meta_key=”Gender”]
[um_user user_id=” ” meta_key=”email”]
-
Hi @kayapati
You don’t need to put the attribute
user_idwhen it is empty. IF you remove the user_id attribute, it will use the logged-in user’s ID.Just use the following syntax:
[um_user meta_key=”country”]
[um_user meta_key=”Gender”]
[um_user meta_key=”email”]Regards,
[um_user meta_key=”country”]
[um_user meta_key=”gender”]I added above but no luck.
I am trying to display meta data of That particular user profile info, not logged in users details.
Just how the info display when we code this function in php file.
do_action(“um_profile_content_main”, $args);
Hi @kayapati
I got it.
Please try this new version of UM Shortcode below:
/** * 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 = um_profile_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' );It should display the current profile’s meta values with the same syntax below:
[um_user meta_key=”country”] [um_user meta_key=”gender”]Regards,
Champ
Sorry the code is not workig.
I added this code in theme function.php
/** * 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 = um_profile_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' );and add the following shortcode in profile form.
[um_user meta_key=”country”] [um_user meta_key=”gender”] [um_user meta_key=”talent_skills”]Hi @kayapati
Could you please remove the
get_current_user_id()from the following lines:$atts = extract( shortcode_atts( array( 'user_id' => get_current_user_id(), 'meta_key' => '', ), $atts ) );Change it to:
$atts = extract( shortcode_atts( array( 'user_id' => 0, 'meta_key' => '', ), $atts ) );Let me know if this works.
Regards,
Added still not working.
This is code in theme function.php
function um_user_shortcode( $atts ) { $atts = extract( shortcode_atts( array( 'user_id' => 0, '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' );and the shortcode is this:
[um_user meta_key=”country”] [um_user meta_key=”gender”] [um_user meta_key=”user_email_18_22”]The meta fields are not displaying.
Note: I am adding the shortcode in User Profile Form at the bottom of the form.
Hi @kayapati
You need to keep the
if( empty( $user_id ) ) $user_id = um_profile_id();Please see the code snippet below:
function um_user_shortcode( $atts ) { $atts = extract( shortcode_atts( array( 'user_id' => 0, 'meta_key' => '', ), $atts ) ); if ( empty( $meta_key ) ) return; if( empty( $user_id ) ) $user_id = um_profile_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' );Regards,
Champ Still not working.
Is it working from your side?
Its getting empty.
Hi @kayapati
Yes, it is working on our end.
Could you please try checking your current profiles fields? Maybe they are empty.
Regards,
No they are filled. and also those are displaying fine above this shortcode.
For clarifying where I am using the shortcode
https://kayapati.com/demos/alexia/user/tesdt/
In above url I used A Print Screen (just click the compcard button to know) of hidden images and fields which are from shortcode.
RIght now I am duplicating the meta fields as the shortcode is not working.
If shortcode works, I just add shortcode and the user do not need to reenter the fields in compcard screen .
Hi @kayapati
Have you made any customization aside from the shortcode? Could you please try disabling them leaving the code for the shortcode?
It works on our test site.
Regards,
Nothing changed, just used what ever you provided.
Are you using the shortcode in profile form?
Check this image to get an idea how I am using
This is added in function.php
function um_user_shortcode( $atts ) { $atts = extract( shortcode_atts( array( 'user_id' => 0, 'meta_key' => '', ), $atts ) ); if ( empty( $meta_key ) ) return; if( empty( $user_id ) ) $user_id = um_profile_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' );This is the shortcode used in my profile form:
[um_user meta_key='country'] [um_user meta_key=”gender”] [um_user meta_key=”hair_color”]Sorry, it seems working, but only the top one country is displaying and others are not displaying, any thing missing?
Hi @kayapati
I’ve added the shortcode to the Profile Form and outside the form which is directly added to the User Page:
https://drive.google.com/file/d/1Su5Rgl0gowswrVoZE5tmb7GCrPDcuxsf/view?usp=sharingBoth ways work on our end.
Regards,
The topic ‘user meta data with shortcode’ is closed to new replies.