WordPress: Checkbox, Radio and Select Helper Functions

As you may or may not know, WordPress has two simple helper functions built into it that make it easy to determine whether a checkbox/radio button should be checked or a select option should be selected.

In PHP, the traditional way to do this would be to use an if…else statement. That might look something like:

<input type="checkbox" value="<?php
    echo $_value
?>"<?php
    if( $my_value == $_value ) {
        echo ' checked="checked"';
    } ?> />

or maybe:

<input type="checkbox" value="<?php
    echo $_value
?>"<?php
    echo ( $my_value == $_value ) ?
        ' checked="checked"' :
        ''; ?> />

However, with WordPress, you can use the checked() or selected() helper functions to make your life a little easier. The checked() helper function can be used with checkbox and radio inputs, while the selected() helper function can be used with select options.

Both functions accept three arguments:

  1. $checked – The first of the two values being compared (generally the value of the checkbox/option itself)
  2. $current [optional] – The second of the two values being compared (generally the current or default value for the group of options). Defaults to boolean true.
  3. $echo [optional] – Whether to echo the checked="checked" or selected="selected" code (boolean true) or simply return it (boolean false)

To use the checked() helper function, you would simply do something like:

<input type="checkbox" value="<?php
    echo $_value
?>"<?php
    checked( $_value, $my_value ) ?> />

To use the selected() helper function, you would simply do something like:

<option value="<?php
    echo $_value
?>"<?php
    selected( $_value, $my_value ) ?>><?php
    echo $_label
?></option>

Unfortunately, neither of these functions are set up to handle arrays of values, so you can only use them when comparing values one-to-one. I would like to see some sort of array comparison introduced to both functions, but I’m sure there are potential conflicts that could occur if that feature was introduced.

Regardless, if you want to try creating your own helper functions that do allow arrays, you could try to use some functions similar to:

<?php
/**
 * Recursive checked helper function
 * @param mixed $checked the value of the checkbox/radio button
 * @param mixed $current the value to compare to the checkbox value
 * @param bool $echo whether or not to echo the output
 *
 * If either $current or $checked is an array, this function will search
 * 		for the other value within the array. If they are both strings,
 * 		or they are both arrays, they will be compared one-to-one (as strings).
 */
function rchecked( $checked, $current=true, $echo=true ) {
	if( !is_array( $current ) && !is_array( $checked ) )
		return __checked_selected_helper( $checked, $current, $echo, 'checked' );
	if( is_array( $current ) && is_array( $checked ) )
		return __checked_selected_helper( $checked, $current, $echo, 'checked' );

	if( is_array( $current ) ) {
		return __rchecked_selected_helper( $checked, $current, $echo, 'checked' );
	} else {
		return __rchecked_selected_helper( $current, $checked, $echo, 'checked' );
}

/**
 * Recursive selected helper function
 * @param mixed $selected the value of the select option
 * @param mixed $current the value to compare to the option value
 * @param bool $echo whether or not to echo the output
 *
 * If either $current or $selected is an array, this function will search
 * 		for the other value within the array. If they are both strings,
 * 		or they are both arrays, they will be compared one-to-one (as strings).
 */
function rselected( $selected, $current=true, $echo=true ) {
	if( !is_array( $current ) && !is_array( $selected ) )
		return __checked_selected_helper( $selected, $current, $echo, 'selected' );
	if( is_array( $current ) && is_array( $selected ) )
		return __checked_selected_helper( $selected, $current, $echo, 'selected' );

	if( is_array( $current ) ) {
		return __rchecked_selected_helper( $selected, $current, $echo, 'selected' );
	} else {
		return __rchecked_selected_helper( $current, $selected, $echo, 'selected' );
}

/**
 * Recursive checked/selected helper function
 * @param string $helper the value of the select option or checkbox/radio input
 * @param mixed $current the value to compare to the option value
 * @param bool $echo whether or not to echo the output
 *
 * If either $current is an array, this function will search for the $helper value
 * 		within $current. If it is a string, they will be compared one-to-one
 * 		(as strings).
 */
function __rchecked_selected_helper( $helper, $current, $echo, $type ) {
	if( !is_array( $current ) )
		return __checked_selected_helper( $helper, $current, $echo, $type );

	$helped = in_array( $helper, $current, true ) ? " $type=\"$type\"" : '';
	if( $echo )
		echo $helped;

	return $helped;
}
?>

Did you like this post? Get monthly summary of our new tutorials, posts and tips to your inbox!

No Responses

Post Your Comment

Your email address will not be published.