Add filters to select array
- This topic has 2 replies, 2 voices, and was last updated 8 years, 2 months ago by
carassius.
-
AuthorPosts
-
February 15, 2017 at 8:33 AM #5096
carassius
ParticipantHi all,
I want to make my plugin extendable and this means adding actions or filters to arrays for my select field options, but I cannot seem to make it work.
For example this is my array
'options' => array( 'ea' => 'Each', 'hr' => 'Hourly', 'day' => 'Daily', 'wk' => 'Weekly', 'mth' => 'Month', 'yr' => 'Yearly', ),
I tried adding
do_action('unit_options)
after the 'yr' but when I tried adding a function to add to it then it didnt echo and I am sure it is how I wrote the functionfunction add_unit_options() { $new_unit = "'qtr' => 'Quarterly'"; retuen $new_unit; } do_action( 'unit_options', 'add_unit_options' );
I was also thinking about making repeatable fields in my settings page so people can build out the array themselves, any ideas?
February 15, 2017 at 9:39 AM #5099Anh Tran
KeymasterHi,
I think you can do like this in your plugin:
$options = array( 'ea' => 'Each', 'hr' => 'Hourly', 'day' => 'Daily', 'wk' => 'Weekly', 'mth' => 'Month', 'yr' => 'Yearly', ); $options = apply_filters( 'prefix_unit_options', $options );
Then when registering meta boxes, just specify
'options' => $options
.It might be a better idea to wrap the value of options to a function like:
function prefix_get_unit_option() { $options = ...; return apply_filters( 'prefix_unit_options', $options ); }
And use
'options' => prefix_get_unit_option()
.This way you can make a repeatable field in your settings page and get the values, passing them to the
prefix_get_unit_option()
to build the array of options.function prefix_get_unit_option() { $options = ...; // Default options $settings = get_option( 'option_name' ); if ( ! empty( $settings['unit_options'] ) ) { foreach ( $settings['unit_options'] as $value => $label ) { $options[$value] = $label; } } return apply_filters( 'prefix_unit_options', $options ); }
February 15, 2017 at 1:15 PM #5103carassius
ParticipantAll great
function p52_contact_get_relationship_options( $options = array() ) { $options = array( 'prospect' => 'Prospect', 'lead' => 'Lead', 'client' => 'Client', 'agency' => 'Agency', 'assitant' => 'Assistant', 'supplier' => 'Supplier' ); $settings = get_option( 'p52crm_settings' ); if ( ! empty( $settings['contact_relationship'] ) ) { foreach ( $settings['contact_relationship'] as $value => $label ) { $options[$value] = $label; } } return apply_filters( 'p52_contact_relationship_options', $options ); }
array( 'name' => 'Relationship', 'id' => $prefix . 'relationship', 'type' => 'select', 'options' => p52_contact_get_relationship_options(), 'placeholder' => 'Choose your relationship', 'columns' => 6 ),
-
AuthorPosts
- The topic ‘Add filters to select array’ is closed to new replies.