Add filters to select array

Support General Add filters to select array

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #5096
    carassiuscarassius
    Participant

    Hi 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 function

    function 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?

    #5099
    Anh TranAnh Tran
    Keymaster

    Hi,

    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 );
    }
    #5103
    carassiuscarassius
    Participant

    All 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
                ),
Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘Add filters to select array’ is closed to new replies.