Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] detecting if buddypress profile field has been completed?

  • Avatar of Joel
    Joel
    Participant

    @knu2xs

    I am attempting to try and display a buddypress profile field I call What makes me tick… using an if/else statement in the content.php file where typically the author description would be displayed.

    I am successfully retrieving the field. This part works. However, what I would like to do is display alternate text if the user has not entered anything in this field. The result is both the profile information and the alternate text being displayed.

    My background is with Python. This is my first foray into working with php. Any help, pointers and best practices suggestions are greatly appreciated.

    Here is the relevant code from the bottom of the content.php file:

    <?php
    	$bpProfile_tick = bp_profile_field_data( 'field=What makes me tick...&user_id=' . get_the_author_meta( 'ID' ) );
    	if ( $bpProfile_tick ) : // If a user has filled out their description
    ?>
    	<p><?php echo $bpProfile_tick; ?></p>
    <?php else: ?>
    	<p>Apparently <?php echo get_the_author() ?> does not feel like sharing anything. Isn't this sad?</p>
    <?php endif; ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Avatar of shanebp
    shanebp
    Moderator

    @shanebp

    You can google BuddyPress functions or grep the source or check the codex.

    bp_profile_field_data will echo the result.

    You want to use bp_get_profile_field_data which just returns the result.
    Then you need to tweak your conditional to check for ‘empty’ rather than a boolean.
    if ( empty ( $some_var ) )

    Also note the apply_filters hook in that function ( and many others)
    I’ll bet you’ll find that interesting.

    Avatar of Joel
    Joel
    Participant

    @knu2xs

    Shane, you rock! Your suggestion worked perfectly. Below is the working code…just in case somebody else searches for this or a very similar issue. Again, thank you for explaining this and pointing me toward the relevant resources, Shane.

    <?php
    $bpProfileField = bp_get_profile_field_data( 'field=What makes me tick...&user_id=' . get_the_author_meta( 'ID' ) );
    if ( empty ( $bpProfileField) ):
    ?>
    <p>Apparently <?php echo get_the_author() ?> does not feel like sharing anything. Isn't this sad?</p>
    <?php else: ?>
    <p><?php echo bp_profile_field_data( 'field=What makes me tick...&user_id=' . get_the_author_meta( 'ID' ) ) ?></p>
    <?php endif; ?>

    Avatar of shanebp
    shanebp
    Moderator

    @shanebp

    You don’t need to query the field_data twice

    <?php
    $bpProfileField = bp_get_profile_field_data( 'field=What makes me tick...&user_id=' . get_the_author_meta( 'ID' ) );
    if ( empty ( $bpProfileField) ):
    ?>
    <p>Apparently <?php echo get_the_author() ?> does not feel like sharing anything. Isn't this sad?</p>
    <?php else: ?>
    <p><?php echo $bpProfileField; ?></p>
    <?php endif; ?>
    Avatar of Joel
    Joel
    Participant

    @knu2xs

    Thanks for the catch. I just built the code around a working line of code and forgot to replace it in my haste. It always helps to have another set of eyes take a look! Thanks again for your through assistance.

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

You must be logged in to reply to this topic.