Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 59525

user meta data with shortcode

$
0
0

Replies: 0

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”]

this is the screenshot:


Viewing all articles
Browse latest Browse all 59525

Trending Articles