-
-
Save danieljwonder/9384b50d5fa050fa7b7fea4fb654928c to your computer and use it in GitHub Desktop.
Search & Filter Pro - Filter Input Object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function filter_input_object($input_object, $sfid) | |
| { | |
| var_dump($input_object); | |
| //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 | |
| if(($input_object['name']!='_sfm_colours')||($input_object['type']!='select')) | |
| { | |
| return $input_object; | |
| } | |
| //we want to filter the options generated, so make sure the options variable actually exists before proceeding | |
| if(!isset($input_object['options'])) | |
| { | |
| 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) | |
| { | |
| if($option->value=="") | |
| {//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.")"; | |
| } | |
| } | |
| //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 are optional - must be assoc arrays - these are mapped to the html output | |
| $new_last_option->attributes = array( | |
| 'class' => 'custom_class another_custom_class', | |
| 'title' => 'My Custom Title', | |
| 'style' => 'background-color: #aa6666;', | |
| 'onclick' => 'alert("this is horrible JS")' | |
| ); | |
| //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) ); | |
| //add/override prefix & postfix to the field | |
| $input_object['prefix'] = "Prefix"; | |
| $input_object['postfix'] = "Postfix"; | |
| //manually set the values shown in the field - must be array even if single value | |
| //this should really only occur under specific conditions, otherwise you field will be permanently set to this value | |
| $input_object['defaults'] = array("black"); | |
| //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:"; | |
| 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