This one’s for developers! If you’re using checkboxes, radio buttons, or dropdowns in your theme or plugin options, you might have overlooked some very useful functions that aren’t listed in the Codex: checked()
,selected()
, and disabled()
. These allow you to compress the code required to see whether or not the relevant option has already been selected, or whether the form option should be disabled. This results in much more readable code.
1
2
3
4
5
|
// Testing the value with if()
<input type= "checkbox" name= "options[postlink]" value= "1" <?php if ( $options [ 'postlink' ] == 1 ) echo 'checked="checked"' ; ?> />
// Using checked() instead
<input type= "checkbox" name= "options[postlink]" value= "1" <?php checked( $options [ 'postlink' ], 1); ?> />
|
Checked()
works with radio buttons as well as checkboxes.
In a long list of dropdown options, the difference withselected()
is even more striking.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
// Testing the value with if()
<select name= "options[content]" >
<option value= "title" <?php if ( $options [ 'content' ] == 'title' ) echo 'selected="selected"' ; ?>>Title Only</option>
<option value= "excerpt" <?php if ( $options [ 'content' ] == 'excerpt' ) echo 'selected="selected"' ; ?>>Title and Excerpt</option>
<option value= "content" <?php if ( $options [ 'content' ] == 'content' ) }> echo 'selected="selected"' ; ?>>Title and Content</option>
</select>
// Using selected() instead
<select name= "options[content]" >
<option value= "title" <?php selected( $options [ 'content' ], 'title' ); ?>>Title Only</option>
<option value= "excerpt" <?php selected( $options [ 'content' ], 'excerpt' ); ?>>Title and Excerpt</option>
<option value= "content" <?php selected( $options [ 'content' ], 'content' ); ?>>Title and Content</option>
</select>
|
The third function, disabled()
, is used to disable a button if the two given values match. Here’s an example from the core:
1
2
3
4
5
|
// using if()
<td><input name= "hostname" type= "text" id= "hostname" value= "<?php echo esc_attr($hostname); if ( !empty($port) ) echo " : $port "; ?>" <?php if ( defined( 'FTP_HOST' ) ) echo ' disabled="disabled"' ?> size= "40" /></td>
// using disabled() instead
<td><input name= "hostname" type= "text" id= "hostname" value= "<?php echo esc_attr($hostname); if ( !empty($port) ) echo " : $port "; ?>" <?php disabled( defined( 'FTP_HOST' ) ); ?> size= "40" /></td>
|
Find all the WordPress Hidden Gems in [link id="2675"]Beginning WordPress 3[/link].
Thanks loads for this. Put it to immediate use.
Your
checked()
above might need a minor correction tho: there’s a black space before the php (after thevalue=
).That space isn’t needed, as it’s conditionally added by WP’s
__checked_selected_helper()
. If match is true, that function returns:" $type='$type'"
(where $type equals'selected'
,'checked'
or'disabled'
). So that space will be writ if needed.Your selected() and disabled() examples above, correctly, do not have that space.
Again, thanks much for this, and the other hidden gems you’ve revealed.