Skip to content

Instantly share code, notes, and snippets.

@isotrope
Created April 15, 2015 20:23
Show Gist options
  • Save isotrope/bd27c634ed95a621a490 to your computer and use it in GitHub Desktop.
Save isotrope/bd27c634ed95a621a490 to your computer and use it in GitHub Desktop.
Feed in dynamic options to CFS's Select field type
<?php
/**
* Hooking into the CFS Select field and filling it with whatever options we want
*/
/**
* Unfortunately, there only seems to be a hook for ALL OF THE FIELDS!
*/
add_filter( 'cfs_pre_render_fields', 'iso_override_select' );
function iso_override_select( $input_fields ) {
/**
* We'll be getting all the info for all fields declared
*
* Let's get through all of them...
*/
foreach ( $input_fields as $input_field ) {
/**
* ...until we find the one that we want by "name" AKA the field slug
*
* In this example, the field's slug was 'select_test'
*/
if ( 'select_test' === $input_field->name ) {
/**
* Found it!!
*
* Let's overwrite the values that show up
*
*/
/**
* THIS IS WHERE YOU'D GET OR FEED IN ALL YOUR SQL RETURNED STUFF
* AS A OPTION VALUE => PRETTY DISPLAY PAOR
*
* As an example, I'm simply declaring an array in here...
*/
// get the values you want into this array
$values = array(
'something_value' => 'Something pretty text',
'something_else_value' => 'Something else pretty text',
'michal_rocks' => 'Michal rocks!',
'ansel_rocks' => 'Ansel is totes awesome!'
);
/**
* If ever you want the values entered in the UI side AND the ones we got just above,
* simply do an array_merge of $values and $input_field->options['choices']
*
* Otherwise.... (overwriting)
*/
$input_field->options['choices'] = $values;
}
// That's it, but if you wanted to affect other fields, this would be the place to do it.
// try doing a var_dump($input_field) here.
// It should shed light on how the other field types behave / what info they contain
}
return $input_fields;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment