BuddyDev

Search

php – hide search form field based on WordPress user role

  • Participant
    fabian on #45502

    Hi,
    I heard lot of good things about the Buddydev community.
    My friends on Facebook recommended to come here to ask if I need any help with Buddypress because
    apparently you are absolute experts with BuddyPress 🙂

    So, this is why I am here hoping you might be able to help me:

    I use a search form and I try to hide specific fields per user role.

    So for example a subscriber cannot see a field that a contributor can see or an administrator, etc.
    This way I can try to increase membership level sign-ups.

    I really hope you can lead me in the right direction here at Buddydev.
    Thank you 😉

  • Keymaster
    (BuddyDev Team)
    Posts: 24593
    Brajesh Singh on #45511

    Hi Fabien,
    Welcome to BuddyDev support forums.

    Thank you for the kind words.

    Are you using any plugin to add the search form or is it some custom coded feature?

    Please share the details and we will assist you.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 5
    Fabian on #45522

    Good afternoon Brajesh 🙂

    Yes you are right, I use BP Search plugin – this search form has fields that I cannot set the visibility for in the backend (unfortunately).
    So I asked in other forums but nothing worked.
    This is how far I came:

    function hide_field_19_wrap ($F) {
        
    	global $current_user;
    	if ( current_user_can( $current_user, "subscriber" ) ) 
            
    		unset ($fields['19']);
     		return $fields;
    }
    
    add_filter ('bps_before_search_form_filter' , 'hide_field_19_wrap', 10, 1);

    I know that this is not your plugin which is why I am aware that you dont have any obligation to help but I heard you are a very good coder and I was hoping you might have an idea.
    In this code I try to make field #19 disappear for subscribers.

    Anyway, I really appreciate your help Brajesh
    Stay safe:)

  • Keymaster
    (BuddyDev Team)
    Posts: 24593
    Brajesh Singh on #45534

    Hi Fabien,
    Thank you.

    Please allow me to check the BP Profile Search plugin and get back to oyu in next 24 hours with a solution(hopefully).

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 5
    Fabian on #45560

    Hello Brajesh,
    thank you for your reply 🙂
    Its fine, take your time and if you dont find any solution – dont worry.
    I know its not your plugin so I was just putting my hopes into you 😉

  • Keymaster
    (BuddyDev Team)
    Posts: 24593
    Brajesh Singh on #45574

    Hi Fabian,
    I am sorry for the delaye.

    Please use the following code and you can specify the field ids to be excluded.

    
    
    /**
     * Filter display of BP Profile search form fields by role.
     */
    function buddydev_enable_conditional_profile_search_form_fields( $form ) {
    
    	if ( ! is_user_logged_in() ) {
    		return $form;// update as you need. we are not restricting.
    	}
    
    	// Use comma separated field ids to exclude for the given role.
    	$roles_excluded_fields = array(
    		'subscriber'    => array(1,3),
    		'contributor'   => array(),
    		'author'        => array(),
    		'editor'        => array(),
    		'administrator' => array( ),
    	);
    
    	// I am assuming that user has only one role.
    	$role            = current( wp_get_current_user()->roles );
    	$excluded_fields = isset( $roles_excluded_fields[ $role ] ) ? $roles_excluded_fields[ $role ] : array();
    
    	if ( empty( $excluded_fields ) ) {
    		return $form;
    	}
    
    	for ( $i = 0; $i < count( $form->fields ); $i ++ ) {
    		if ( empty( $form->fields[ $i ] ) || empty( $form->fields[ $i ]->id ) ) {
    			continue;
    		}
    		if ( in_array( $form->fields[ $i ]->id, $excluded_fields ) ) {
    			unset( $form->fields[ $i ] );
    		}
    	}
    }
    add_action( 'bps_before_search_form', 'buddydev_enable_conditional_profile_search_form_fields' );
    add_action( 'bps_before_filters', 'buddydev_enable_conditional_profile_search_form_fields' );
    
    

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved