• Resolved eriksson1337

    (@eriksson1337)


    Hello,

    is it possible to use the xProfile fields of a logged in user as the default values in the search form?

    And if i would have to write a custom form template to make it work, could it also work combined with the Conditional Profile Fields for BuddyPress plugin?

    Thank you

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Andrea Tarantini

    (@dontdream)

    Hi eriksson1337,

    Yes, you can add this code to your file bp-custom.php:

    // replace 5 with the field ID of your field
    
    add_action ('bps_field_before_search_form', 'set_defaults');
    function set_defaults ($f)
    {
    	$user = bp_loggedin_user_id ();
    	if (empty ($user))  return;
    
    	if ($f->code == 'field_5' && empty ($f->value))
    	{
    		$f->value = addslashes (xprofile_get_field_data (5, $user));
    	}
    }

    This code is independent of the form template, so if your template works with ‘Conditional Profile Fields for BuddyPress’ it will continue to do so.

    Thread Starter eriksson1337

    (@eriksson1337)

    Thanks for your reply.

    Would it also be possible (for example) to match the users own field 5 with the other users field 6?

    And is there any solution yet for making BP Profile Search work with Conditional Profile Fields for BuddyPress without having to write a custom form template?

    Plugin Author Andrea Tarantini

    (@dontdream)

    Hi eriksson1337,

    You can modify the previous code to do that:

    // search field 6 defaults to logged-in user's field 5 value
    
    add_action ('bps_field_before_search_form', 'set_defaults');
    function set_defaults ($f)
    {
    	$user = bp_loggedin_user_id ();
    	if (empty ($user))  return;
    
    	if ($f->code == 'field_6' && empty ($f->value))
    	{
    		$f->value = addslashes (xprofile_get_field_data (5, $user));
    	}
    }

    About your second question, yes, you need to write a custom form template using the markup required by ‘Conditional Profile Fields for BuddyPress’.

    Thread Starter eriksson1337

    (@eriksson1337)

    Thank you.

    Thread Starter eriksson1337

    (@eriksson1337)

    Hello,

    setting the default values works great, except of that it does not work with checkboxes. Have you got a solution for that?

    Thank you

    Plugin Author Andrea Tarantini

    (@dontdream)

    Hello eriksson1337,

    When the field is a ‘Checkboxes’ type, the value is an array, so the code becomes:

    // 'Checkboxes' search field 6 defaults to logged-in user's field 5 value
    
    add_action ('bps_field_before_search_form', 'set_defaults');
    function set_defaults ($f)
    {
    	$user = bp_loggedin_user_id ();
    	if (empty ($user))  return;
    
    	if ($f->code == 'field_6' && empty ($f->value))
    	{
    		$f->value = array_map ('addslashes', xprofile_get_field_data (5, $user));
    	}
    }
    Thread Starter eriksson1337

    (@eriksson1337)

    It works, thank you.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Own xProfile Fields as default values in the search form – and conditions’ is closed to new replies.