Support » Plugin: Ultimate Member ā€“ User Profile, User Registration, Login & Membership Plugin » how to show user id in profile and in shortcode

  • Resolved makmerghen

    (@makmerghen)


    Dear
    I want to show user id in profile field and in shortcode to show it in any page
    so I did the next
    I create text field in profile field has meta key = user_id
    but it didn’t show any thing
    also when used this shortcode [um_field_data field=user_id] (it has snippets code and work as example if I use user_login)

    so please advise how to fix and to show the user id
    best regards

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Contributor champsupertramp

    (@champsupertramp)

    Hi @makmerghen

    Could you please share the code for the shortcode here so we can review it?

    Regards,

    Thread Starter makmerghen

    (@makmerghen)

    Dear @champsupertramp
    this is the code

    // Display field data of Ultimate Member
    // e.g. [um_field_data userid="10' field="company_name"]
    add_shortcode( 'um_field_data', 'um_custom_field_shortcode' );
    
    if ( ! function_exists( 'um_custom_field_shortcode' ) ) {
    	function um_custom_field_shortcode( $atts = array() ){
    
    		$defaults = array(
    				'field' => NULL,
    				'userid' => NULL,
    		);
    
    		$atts = wp_parse_args( $atts, $defaults );
    		extract( $atts );
    
    		ob_start();
    
    		$user_id = um_user($atts['userid']);
    		um_fetch_user( $user_id );
    		$meta_value = um_user($atts['field']);
    
    		print_r($meta_value);
    
    	    $shortcode_content = ob_get_contents();
    	    ob_end_clean();
    
    	    return $shortcode_content;
    	}
    }

    best regards

    Plugin Contributor champsupertramp

    (@champsupertramp)

    Hi @makmerghen

    Try this code snippet:

    add_shortcode( 'um_field_data', 'um_custom_field_shortcode' );
    
    if ( ! function_exists( 'um_custom_field_shortcode' ) ) {
    	function um_custom_field_shortcode( $atts = array() ){
    
    		$defaults = array(
    				'field' => NULL,
    				'userid' => um_profile_id(),
    		);
    
    		$atts = wp_parse_args( $atts, $defaults );
    		extract( $atts );
    
    		ob_start();
                    if( "user_id" == $atts['field'] ){
                        return $atts['userid'];
                    }
     
    		$user_id = um_user($atts['userid']);
    		um_fetch_user( $user_id );
    		$meta_value = um_user($atts['field']);
    
    		print_r($meta_value);
    
    	    $shortcode_content = ob_get_contents();
    	    ob_end_clean();
    
    	    return $shortcode_content;
    	}
    }

    Usage: [um_field_data field=user_id] – returns the currently logged-in user’s ID or the profile ID of the current viewing profile.

    Regards,

    Thread Starter makmerghen

    (@makmerghen)

    Dear @champsupertramp thanks a lot for the code
    it works only in shortcode and it appear the user id
    but
    in profile form the user id field still disappeared and if I enter edit profile I found the field as empty text field

    so please advise
    best regards

    Plugin Contributor champsupertramp

    (@champsupertramp)

    Hi @makmerghen

    Did you create a text field to display the user ID? Or did you add the Content Block / Shortcode block in the Profile Form via UM Form Builder?

    Regards,

    Thread Starter makmerghen

    (@makmerghen)

    Dear @champsupertramp
    I added as text
    even after I remove the text field and I add shorcode field
    it is still disappeared in profile and even in edit profile page

    so please advise
    best regards

    Plugin Contributor champsupertramp

    (@champsupertramp)

    Hi @makmerghen

    Are you trying to retrieve the currently logged-in user’s ID? or the profile that you’re viewing?

    Please try this one:

    add_shortcode( 'um_field_data', 'um_custom_field_shortcode' );
    
    if ( ! function_exists( 'um_custom_field_shortcode' ) ) {
    	function um_custom_field_shortcode( $atts = array() ){
    
    		$defaults = array(
    				'field' => NULL,
    				'userid' => um_profile_id(),
    		);
    
    		$atts = wp_parse_args( $atts, $defaults );
    		extract( $atts );
    
    		ob_start();
                    if( "user_id" == $atts['field'] ){
                        return $atts['userid'];
                    }
     
    		um_fetch_user( $atts['userid'] );
    		$meta_value = um_user($atts['field']);
    
    		print_r($meta_value);
    
    	    $shortcode_content = ob_get_contents();
    	    ob_end_clean();
    
    	    return $shortcode_content;
    	}
    }

    Regards,

    Thread Starter makmerghen

    (@makmerghen)

    Dear @champsupertramp
    sorry dear it was mistake from my side
    both codes work so please recommend which one I should use
    best regards

    Plugin Contributor champsupertramp

    (@champsupertramp)

    Thread Starter makmerghen

    (@makmerghen)

    Dear @champsupertramp
    thank you very much and best regards

    Plugin Contributor champsupertramp

    (@champsupertramp)

    Thanks for letting us know.

    Regards,

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘how to show user id in profile and in shortcode’ is closed to new replies.