Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] bp_field_has_data show message when empty field

  • Hi,

    I am trying to create a message in the xprofile view tab, to show only if certain fields are empty, but I can’t get it to work whatever i do. i’m placing the following code inside the profile loop:

    <?php if ( !bp_field_has_data('field_id=2') ) { 
                                                         echo "Please tell the community a little bit about yourself by filling out the form on the 'edit' tab above.";
                                                    } elseif ( !bp_field_has_data('field_id=3') && !bp_field_has_data('field_id=4') ) { 
                                                         echo "There's nothing here! spruce up your profile by adding content via the 'edit' tab above.";
                                                    } else {
                                                         echo "";
                                                    } ?>

    I’d be grateful if someone could point me in the right direction. I’ve trawled the internet for a solution, but i’m getting nowhere.

    Thanks,

    Yukon

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

  • danbp
    Moderator

    @danbp

    hi @jimmmy

    Give this a try. Copy/paste into your theme’s functions.php or into bp-custom.php

    • – The code will only apply on the members profile page.
    • – Add if /endif for each field you need
    • – The message will appear under the username. Do get this on the bp-default theme, The first echo containing BR is probably mandatory.
    • – The field name is case sensitive.
    function bpfr_my_message_on_profile () {	
    	
    	echo '<br>Test my message<br>'; 
    	
    	if ( !$data = bp_get_profile_field_data( 'field=your_field_name_1' ) ) : 
    	
    	echo xprofile_get_field_data( 'your_field_name', bp_displayed_user_id() ) .'The message to display 1<br>';
    	
    	endif;	
    	
    	if ( !$data = bp_get_profile_field_data( 'field=your_filed_name_2' ) ) : 
    	
    	echo xprofile_get_field_data( 'your_field_name_2', bp_displayed_user_id() ) .'The message to display 2<br>';
    	
    	endif;
    }
    add_filter( 'bp_before_member_header_meta', 'bpfr_my_message_on_profile' );

    @danbp Thank you man, this is amazing!

    It didn’t do exactly what i wanted, so i made a couple of tweaks. I changed the filter so the messages were added to the profile only, not the member header meta. I also made the statement into elseif, with an array, so it would do what i needed it to, ie show up only if both fields were empty. Here’s my code. It is probably a bit messy, but it does the trick:

    function bpfr_my_message_on_profile () {	
    	
            if ( bp_is_my_profile()) :
    	
    	if ( !$data = bp_get_profile_field_data( 'field=About' ) ) : 
    	
    	echo '<p class="enter-info">Please tell the community a little bit about yourself by filling out the form on the edit tab above.</p>';	
    	
    	elseif ( !$data = bp_get_profile_field_data(array( field=>'vimeo',field=>'youtube') ) ) : 
    	
    	echo '<p class="enter-info">There is nothing here! Spruce up your profile by adding content via the edit tab above.</p>';
    	endif;
    	endif;
    }
    add_filter( 'bp_before_profile_content', 'bpfr_my_message_on_profile' );

    Thanks for all your help. You rock!


    somethingelse
    Participant

    @somethingelse

    three cheers for both of you!!
    thanks for sharing this. Means now i don’t need yet another plugin!

    Much appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] bp_field_has_data show message when empty field’ is closed to new replies.
Skip to toolbar