WORDPRESS HIDDEN GEMS: CHECKED(), SELECTED(), AND DISABLED()

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 andExcerpt</option>
    <option value="content"<?php if( $options['content'] == 'content') }> echo'selected="selected"'; ?>>Title andContent</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 andExcerpt</option>
    <option value="content"<?php selected( $options['content'],'content'); ?>>Title andContent</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"; ?>"<?phpif( 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].

POST NAVIGATION

4 THOUGHTS ON “WORDPRESS HIDDEN GEMS: CHECKED(), SELECTED(), AND DISABLED()

  1. 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 the value=).

    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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值