• Resolved kayapati

    (@kayapati)


    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 15 replies - 16 through 30 (of 30 total)
  • Thread Starter kayapati

    (@kayapati)

    OK I will check deeply from my end step by step.

    Anyway thank you very much for your help.

    Thread Starter kayapati

    (@kayapati)

    Hi Champ

    I am glad to inform that the shortcode is working fine in my live server, may some thing wrong at local. It’s great support with lots of patience.

    You can tick this as resolved.

    Thread Starter kayapati

    (@kayapati)

    Can I have a label here?

    I mean if the meta field is filled then the Label should be displayed.

    I have added hardcoded label like this:

    Country: [um_user meta_key='country'] | 
    Marital Status: [um_user meta_key='maritalstatus'] | 
    Eye Color: [um_user meta_key='eyecolor'] | 
    Hair Color: [um_user meta_key='haircolor']  | 
    Dress Size: [um_user meta_key='DressSize']

    But when the field is empty the label is displaying.
    https://kayapati.com/demos/alexia/user/pdavim/

    Here you can see the issue, click on DOwnload Compcard and see the shorcode I used in PDF right side below the first image.

    The issue is the Dress Size label is displaying as its meta is not filled.

    • This reply was modified 5 years, 2 months ago by kayapati.
    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @kayapati

    You can try this code snipppet:

    function um_user_shortcode( $atts ) {
    
    	$atts = extract( shortcode_atts( array(
    		'user_id' => 0,
    		'meta_key' => '',
                    'show_label' => false,
    	), $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 );
        }  
        
        if( $show_label && ! empty( $meta_value )  ) {   
            $label = UM()->fields()->get_label( $meta_key );
            $meta_value = "{$label}: {$meta_value}";
        }
    
        return apply_filters("um_user_shortcode_filter__{$meta_key}", $meta_value );
    
    }
    add_shortcode( 'um_user', 'um_user_shortcode' );

    Usage: [um_user meta_key='country' show_label="true"]

    Regards,

    Thread Starter kayapati

    (@kayapati)

    Hi Champ

    The snippet above is working great, and Thanks a lot for your help.

    You can tick this thread as resolved.

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @kayapati

    Thanks for letting us know. I am closing this thread now.

    Regards,

    Thread Starter kayapati

    (@kayapati)

    CHamp

    Here I have small issue, I have added date field in shortcode, but the date field is not displaying number of years like in profile form, in profile form age is displaying number of years as I have choosen the years to display in field settings.

    This is the screnshot.

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @kayapati

    Add this code to format the date-time to Age.

    add_filter("um_user_shortcode_filter__birth_date","um_user_custom__birth_date", 10, 1 );
    function um_user_custom__birth_date( $meta_value ){ 
      UM()->datetime()->get_age( $meta_value );
    }

    Regards,

    Thread Starter kayapati

    (@kayapati)

    Champ

    I added it into theme funciton.php, not displaying the number of years, I am confusing how to call this into shortcode. Can you guide me?

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @kayapati

    Please try this code snippet. There was a typo in my previous message:

    add_filter("um_user_shortcode_filter__birth_date","um_user_custom__birth_date", 10, 1 );
    function um_user_custom__birth_date( $meta_value ){ 
      return UM()->datetime()->get_age( $meta_value );
    }

    The above code changes the meta value to age format for birth_date. It works with the shortcode that you’ve [um_user meta_key="birth_date" show_label="true"].

    Regards,

    Thread Starter kayapati

    (@kayapati)

    It displaying only “49 years old” as fixed number not a dynamic date change when I changed it.

    And the Label is not display for this field.

    Thread Starter kayapati

    (@kayapati)

    Champ can you look at the above issue?

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @kayapati

    Could you please check what the $meta_value returns before it get formatted?

    Regards,

    Thread Starter kayapati

    (@kayapati)

    Before adding the code, it was generating like this: Birth Date: 1974/05/30

    Thread Starter kayapati

    (@kayapati)

    It showing wrong years that too fixed one like this “49 years old” what ever user profile I see the same age.

    This is the screenshot

    • This reply was modified 5 years, 2 months ago by kayapati.
Viewing 15 replies - 16 through 30 (of 30 total)
  • The topic ‘user meta data with shortcode’ is closed to new replies.