Instantly share code, notes, and snippets.

What would you like to do?
Search & Filter Pro - Filter Input Object
function filter_input_object($input_object, $sfid)
//ensure we are only filtering the correct field name - in this case the field we want to filter has the name `_sfm_colours`
//we also want to make sure its a `select` input type we're filtering
return $input_object;
//change parameters in the field
//manually set the values shown/selected in the field - must be array even if single value
//this should really only occur under specific conditions, otherwise your field will be permanently set to this value
$input_object['defaults'] = array("black");
//change classes & attributes of the field itself - must be assoc array - you can add any attributes you can think of or would need
//you should never change the field name or replace the attributes array
$input_object['attributes']['class'] = 'custom_class another_custom_class';
$input_object['attributes']['title'] = 'My Custom Title';
$input_object['attributes']['style'] = 'background-color: #c7d8ff;color:#4662a0;';
$input_object['attributes']['onclick'] = 'console.log("this is horrible JS")';
//add/override prefix & postfix to the field
$input_object['prefix'] = "Prefix";
$input_object['postfix'] = "Postfix";
//this will only be used on select fields and text input types - do not use - this variable will be renamed soon to `screen_reader_text`
//$input_object['accessibility_label'] = "Screen Reader Text:";
//if we want to filter the options generated, we need to make sure the options variable actually exists before proceeding (its only available to certain field types)
return $input_object;
//now we know there are options we can loop through each one, and change what we need
foreach($input_object['options'] as $option)
{//the option with no value is always the "all items" or unselected state
$option->label = "Choose a Colour:";
else if($option->value=="black")
{//we want to change the label for the option "black" - we can feed back in the count number to the label for this field type
$option->label = "Jet Black (".$option->count.")";
//yes you can even change the attributes on each option if necessary!
$option->attributes['style'] = "color:#fff;background-color:#000";
$option->attributes['title'] = "This is Jet Black";
//options are just an array so we can do all the usual things with arrays using PHP
//reverse the options list
$input_object['options'] = array_reverse($input_object['options']);
//put the "all items" option back to the top of the array, which was moved to bottom by `array_reverse`
//S&F requires the first item in an options list to always be the unselected state/option
$last_option = array_pop($input_object['options']); //pop last option
array_unshift($input_object['options'],$last_option); //prepend
//create a new option we want to add
//options must have a value and label
$new_last_option = new StdClass();
$new_last_option->value = "my_custom_value";
$new_last_option->label = "This is a New Option";
//attributes on options are optional - must be assoc arrays - these are mapped to the html output
$new_last_option->attributes = array(
'class' => 'custom_option_class another_custom_option_class',
'title' => 'My Custom Title',
'style' => 'background-color: #46a064;color:#ffffff'
//add a brand new option to the bottom
array_push($input_object['options'], $new_last_option);
//create a new option we want to add
//options must have a value and label
$new_first_option = new StdClass();
$new_first_option->value = "another_custom_value";
$new_first_option->label = "New First Option";
//insert option to options array at position 1 (after the default "all items option")
array_splice( $input_object['options'], 1, 0, array($new_first_option) );
return $input_object;
add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment