Skip to:
Content
Pages
Categories
Search
Top
Bottom

Display badges/images based on xprofile field value


  • kida18
    Participant

    @kida18

    Hello!

    I would like to display a badge on the member’s cover photo based on a xprofile field he’s chosen.
    The profile field is a dropdown.
    I’ve searched in many places, including this forum, but I can’t figure it out..
    I’m not a coder, but I’ve put together something based on several topics I’ve found, so please forgive me for any non sense you might see!

    function show_badge() {
    
    	if ( bp_get_the_profile_field_name() == 'Talent' ) {
    		global $field;
    		if ( bp_unserialize_profile_field( $field->data->value ) == "Photography" ) {
    		echo '<div class="badge1"><img src="http://www.mysite.com/wp-content/uploads/photobadge.png"></div>';
    		}
    	}
    }
    
    add_action('bp_init', 'show_user_badge');
    function show_user_badge () {
        if (bp_is_user()){
            add_action('bp_after_member_header', 'show_badge');
        }
    }

    Could someone advise me what is wrong with this code and how I could make it work?
    I would really appreciate any help with that!

    Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)

  • ckchaudhary
    Participant

    @ckchaudhary

    Hi,
    try this:-

    add_action( 'bp_after_member_header', 'myprefix_show_badge' );
    function myprefix_show_badge(){
        if( !bp_is_active( 'xprofile' ) )
            return;
        
        $field_value = xprofile_get_field_data( 'Talent', bp_displayed_user_id() );
        
        if( 'photography' == strtolower( $field_value ) ){
            echo '<div class="badge1"><img src="http://www.mysite.com/wp-content/uploads/photobadge.png"></div>';
        }
    }

    This should work.
    And I, personally, prefer the term ‘programmer’ over ‘coder’ 🙂


    kida18
    Participant

    @kida18

    It worked!!! Thank you!
    I spent so much time on this one it’s great to finally see it working!

    And I agree, ‘programmer’ is probably the better word, my french just took over for a minute :p

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Skip to toolbar