Skip to content

Filters

Javier Prieto edited this page Dec 18, 2020 · 2 revisions

Important: This documentation is still under construction, methods and/or properties may be missing or not fully documented

jp_toolkit_html_helper_form_options

This hook filter the content passed to Form::options() method before construct the HTML elements. Is a quick method to add a shorthand to options. To get more details on how to add your own option shorthand please read this.

apply_filter('jpwp_toolkit_helpers_form_options', array|string $options );

$options (array|string)
An array with the options

return (array)
The updated options array.

Example

The following example the code adds the colors shorthand.

function add_color_options($options) {
    // Always check if $options is your shorthand before process it.
    if ('colors' != $options) {
        return $options;
    }

    $colors = [
        '#FFFFFF' => __( 'White', 'textdomain' ),
        '#808080' => __( 'Gray', 'textdomain' ),
        '#000000' => __( 'Black', 'textdomain' ),
        '#FF0000' => __( 'Red', 'textdomain' ),
        '#FFFF00' => __( 'Yellow', 'textdomain' ),
        '#008000' => __( 'Green', 'textdomain' ),
        '#0000FF' => __( 'Blue', 'textdomain' ),         
    ];

    return $colors;
}

add_filter('jpwp_toolkit_helpers_form_options', 'add_color_options');

Now you can use the colors shorthand to show these options.

echo Form::options( 'colors' );

The code above will generate the following HTML:

<option value="#FFFFFF">White</option>
<option value="#808080">Gray</option>
<option value="#000000">Black</option>
<option value="#FF0000">Red</option>
<option value="#FFFF00">Yellow</option>
<option value="#008000">Green</option>
<option value="#0000FF">Blue</option>

Clone this wiki locally