Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] Only display custom field if not empty


  • mrgiblets
    Participant

    @mrgiblets

    Hey guys,

    Using this in member-header.php :

    <div>Location: <?php bp_member_profile_data( 'field=Location' ); ?></div>

    But I only want to show this div when the Location field is occupied.

    A usual way to do it in WP with a custom field using post meta would be something like :

    <?php if($location = get_post_meta(get_the_ID(), 'location', true)): ?><div>Location: <?php echo $location; ?></div>

    But I don’t know how to check for this particular field within buddypress as I’m not sure how these fields are being stored as variables, or where they are being stored. In fact I haven’t seen any database queries anywhere on my buddypress journey yet which makes a nice change as it happens 😉

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

  • danbp
    Participant

    @danbp

    Here an example which work when you’re in a members loop. You could use from within bp-custom. Note: it’s just a way to do it, may exist others.

    function xyz( $user_id = 0, $field_name = 'Location' ) {
    
    $return = false;
    	
    	if ( empty( $user_id ) || empty( $field_name ) || ! bp_is_active( 'xprofile' ) ) {
    		return $return;
    	}
    
    // the bp way to get user datas
    $location = xprofile_get_field_data( 'Location', $user_id ); 
    
    // output the div only if $location has data
    if ( ! empty( $location ) ) {
    		echo '<div>'. $location .'</div>';
    	}
    }
    
    // we show this on the member header, using an pre-defined action hook in members-header.php
    add_action( 'bp_before_member_header_meta', 'xyz' );

    mrgiblets
    Participant

    @mrgiblets

    Superb, thank you @danbp!

    Not only answered my question but this also gives me the fundamentals to implement further ideas I have for my BP site like displaying profile fields based on user roles.

    Cheers 🙂


    mrgiblets
    Participant

    @mrgiblets

    A quick follow up question…

    Can I echo the xprofile_get_field_data fields elsewhere in WordPress (outside of Buddypress) in the same way?

    ie :

    $location = xprofile_get_field_data( 'Location', $user_id );
    echo $location;

    Obviously replacing user_id with the numeric user id.


    mrgiblets
    Participant

    @mrgiblets

    One other note about the code you provided, It only works if I declare the user_id in the variable. ($user_id = 2), but that’s obviously not practical here. How do I set that to get the user_id of the current profile being viewed?


    danbp
    Participant

    @danbp

    You just need an existing user_id.

    He an example where you get a field value to add in a custom action in one of your template.

    
    function bpfr_field( $custom_field ) {
    	global $bp;
    	
    	// is xprofile component active ?
    	if ( bp_is_active( 'xprofile' ) )
    	
    	// fetch the user_id
    	$current_user = wp_get_current_user();
    	$current_user_id = $current_user->ID;
    	
    	// fetch the data
    	$custom_field = xprofile_get_field_data('42', $current_user_id); // 42 is a field ID
    	
    	// show the data
    	echo '<div class="authorinfo">'. $custom_field . '</div>';
    	
    }
    add_action( 'myfield', 'bpfr_field', 1 );

    Then, add this at the appropriate place in your template <?php do_action( 'myfield'); ?>

    Another example, which you can add at the begin of a custom function.

    global $bp;
    	// Check if the Activity component is active before using it.
    	if ( ! bp_is_active( 'activity' ) ) {
            return;
        }
    	if( bp_is_user() && ! bp_get_member_user_id() ) {
            $user_id = 'displayed: '. bp_displayed_user_id();
        } else {
            $user_id = 'get_member_user: '. bp_get_member_user_id();
        }
    
    // your stuf here

    Note that both examples use global $bp, a check for active component with a different usage for a same result: if not active, the function will do nothing. This allows you to deactivate a component without throwing a warning or a php failure despite the function is in your theme, bp-cutom or plugin.


    mrgiblets
    Participant

    @mrgiblets

    Brilliant that’s perfect, thanks again @danbp 😉


    mrgiblets
    Participant

    @mrgiblets

    Hey @danbp,

    Sorry bro, I can’t get it to return the user_id.

    This is what i’ve got in bp-custom.php :

    function field_location() {
    global $bp;
    	// Check if the Activity component is active before using it.
    	if ( ! bp_is_active( 'activity' ) ) {
            return;
        }
    	if( bp_is_user() && ! bp_get_member_user_id() ) {
            $user_id = 'displayed: '. bp_displayed_user_id();
        } else {
            $user_id = 'get_member_user: '. bp_get_member_user_id();
        }
    	
    $field_name = 'Location';
    $return = false;
    	
    	if ( empty( $user_id ) || empty( $field_name ) || ! bp_is_active( 'xprofile' ) ) {
    		return $return;
    	}
    
    // the bp way to get user datas
    $location = xprofile_get_field_data( 'Location', $user_id ); 
    
    // output the div only if $location has data
    if ( ! empty( $location ) ) {
    		echo '<div style="margin-bottom:5px;">Location: '. $location .'</div>';
    	}
    }
    // we show this on the member header, using an pre-defined action hook in members-header.php
    add_action( 'bp_before_member_header_meta', 'field_location' );

    And then I’m calling it in member-header.php like this :

    <?php field_location(); ?>

    Using do_action( ‘field_location’ ); isn’t working for me but the above method does.

    But it returns nothing. Only if I declare the user_id variable somewhere will it return the data for that user.


    mrgiblets
    Participant

    @mrgiblets

    Doh.. Don’t worry I see what’s happening.. Need more coffee 😉

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Resolved] Only display custom field if not empty’ is closed to new replies.
Skip to toolbar