BuddyDev

Search

How to add condition to custom profile button

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #47682

    Hi,

    I have a custom profile header button with code below but How can I add condition to only display the button when at least one profile field in the group (CONTACT DETAILS) has been added.

    CONTACT DETAILS profile group has the following profile fields
    1 Phone Number
    2 Email Address
    3 Website Url

    I want to display the CONTACT ME button when at least one of the profile field has been added.

     function bp_add_contact_button() {
    
    	if ( !is_user_logged_in() ) {
    		return;
    	}
    
    	$contact_user_button_args = array(
    		'id'                => 'contact_user',
    		'component'         => 'members',
    		'must_be_logged_in' => true,
    		'block_self'        => true,
    		'link_class'        => 'contact-user',
    		'link_href'         => esc_url( bp_displayed_user_domain() . bp_get_profile_slug() . '/#item-body' ),
    		'link_text'         => __( 'Contact Me' ),
    	);
    
    	echo bp_get_button( $contact_user_button_args );
    	
    }
    add_action( 'bp_member_header_actions', 'bp_add_contact_button' );
     
  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #47693

    Hi Tosin,
    Please take a look at the code here.
    https://github.com/buddydev/bp-profile-completion/blob/master/src/core/class-bp-profile-completion-helper.php#L331

    There are no simple or direct answers here. You will need to write a custom db query and detect as shown above.

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #47713

    Hi Brajesh,

    Does this make any sense

     function bp_add_contact_button() {
    
    	// Check if the current user is logged in
    	if ( !is_user_logged_in() ) {
    		return;
    	}
    
    	// Get the current user's ID
    	$user_id = bp_displayed_user_id();
    
    	// Get the last updated timestamp for the "Contact Information" profile group
    	$last_updated = bp_get_user_meta( $user_id, 'bp_profile_group_contact_information_last_updated', true );
    
    	// Check if the "Contact Information" profile group has at least one field updated
    	if ( !empty( $last_updated ) ) {
    		$contact_user_button_args = array(
    			'id'                => 'contact_user',
    			'component'         => 'members',
    			'must_be_logged_in' => true,
    			'block_self'        => true,
    			'link_class'        => 'contact-user',
    			'link_href'         => esc_url( bp_displayed_user_domain() . bp_get_profile_slug() . '/#item-body' ),
    			'link_text'         => __( 'Contact Me' ),
    		);
    		echo bp_get_button( $contact_user_button_args );
    	}
    }
    add_action( 'bp_member_header_actions', 'bp_add_contact_button' ); 
  • Participant
    Level: Guru
    Posts: 900
    Tosin on #47714

    or maybe using a profile group id 4323 to identify CONTACT DETAILS profile group

     function bp_add_contact_button() {
    
    	// Check if the current user is logged in
    	if ( !is_user_logged_in() ) {
    		return;
    	}
    
    	// Get the current user's ID
    	$user_id = bp_displayed_user_id();
    
    	// Get the field data for the "Contact Information" profile group
    	$field_data = bp_get_profile_group_field_data( array( 'user_id' => $user_id, 'group_id' => 4323 ) );
    
    	// Check if the "Contact Information" profile group has at least one field updated
    	if ( !empty( $field_data ) ) {
    		$contact_user_button_args = array(
    			'id'                => 'contact_user',
    			'component'         => 'members',
    			'must_be_logged_in' => true,
    			'block_self'        => true,
    			'link_class'        => 'contact-user',
    			'link_href'         => esc_url( bp_displayed_user_domain() . bp_get_profile_slug() . '/#item-body' ),
    			'link_text'         => __( 'Contact Me' ),
    		);
    		echo bp_get_button( $contact_user_button_args );
    	}
    }
    add_action( 'bp_member_header_actions', 'bp_add_contact_button' ); 
  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #47728

    Hi Tosin,
    I don’t think that any function like

    
    bp_get_profile_group_field_data()
    

    exists for BuddyPress. That’s why I pointed to the other plugin.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved