BuddyDev

Search

Custom User Metadata shortcode does not work on tabs

Tagged: 

  • Participant
    Level: Initiated
    Posts: 4
    Mike Gentile on #53053

    Custom User Metadata shortcode does not work on tabs but works on pages just fine.

  • Keymaster
    (BuddyDev Team)
    Posts: 24435
    Brajesh Singh on #53058

    Hi Mike,
    Welcome to BuddyDev support forums.

    Please share the exact shortcode you are using.

    Thank you
    Brajesh

  • Participant
    Level: Initiated
    Posts: 4
  • Participant
    Level: Initiated
    Posts: 4
  • Participant
    Level: Initiated
    Posts: 4
    Mike Gentile on #53102

    bump

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3110
    Ravi on #53107

    Hello Mike,

    The current implementation of the shortcode is problematic because BuddyPress pages reset the $post variable, rendering it unavailable. To address this issue, it is recommended to modify the shortcode to accept additional attributes, allowing the manual specification of the post ID. However, this approach results in a static configuration, limiting the shortcode’s flexibility with respect to dynamic post IDs.

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 4
    Mike Gentile on #53113

    So what is your recommendation?

    function my_assessment_score($atts) {
    // Parse the shortcode attributes
    $atts = shortcode_atts(array(
    ‘post_id’ => null, // Default to null if no post ID is provided
    ), $atts, ‘assessment_score’);

    // Determine the post ID to use
    $post_id = $atts[‘post_id’] ? intval($atts[‘post_id’]) : get_the_ID();

    // Return the assessment score for the specified post ID
    return get_post_meta($post_id, ‘assessment_score’, true);
    }
    add_shortcode(‘assessment_score’, ‘my_assessment_score’);

    this still didn’t work

  • Keymaster
    (BuddyDev Team)
    Posts: 24435
    Brajesh Singh on #53116

    Hi Mike,
    You will meed something like this

    
    function my_assessment_score( $atts ) {
    // Parse the shortcode attributes
    	$atts = shortcode_atts( array(
    		'post_id' => 0, // Default to 0 if no post ID is provided
    	), $atts, 'assessment_score' );
    
    // Determine the post ID to use
    	$post_id = $atts['post_id'] ? intval( $atts['post_id'] ) : get_the_ID();
    
    // Return the assessment score for the specified post ID
    	return $post_id ? get_post_meta( $post_id, 'assessment_score', true ) : '';
    }
    
    add_shortcode( 'assessment_score', 'my_assessment_score' );
    
    

    Also, for the profile tabs, if you do not pass the post id, It will not show anything.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved