checked()、selected()、disabled()
原文来自:http://pangbu.com/checked_selected_disabled/
checked()、selected()、disabled(),这三个函数是主题设计和插件设计中添加后台设置比较常用到的函数,看似不起眼的小函数却能给我们很大便利。因为很少有人用到,也就很少有文献来说明,更别说是中文说明了,So… 今天我们就是要讲一下这个。
好吧,还是授人鱼吧,我们先不讲原理直接来函数。
checked函数
input:checkbox类型的一个是否被勾选的判断函数
我们在做主题或者插件后台设置的时候可以像如下来使用它
// <input type="checkbox" name="if" value="1" <?php checked(1,get_option('if'));?> /> //
selected函数
select标签中的选项是否被选中
<select id="start_of_week" name="weekday">
<option value="0" <?php selected(0,get_option('weekday'));?> >星期日</option>
<option value="1" <?php selected(1,get_option('weekday'));?> >星期一</option>
<option value="2" <?php selected(2,get_option('weekday'));?> >星期二</option>
<option value="3" <?php selected(3,get_option('weekday'));?> >星期三</option>
<option value="4" <?php selected(4,get_option('weekday'));?> >星期四</option>
<option value="5" <?php selected(5,get_option('weekday'));?> >星期五</option>
<option value="6" <?php selected(6,get_option('weekday'));?> >星期六</option>
</select>
disabled函数
html中各类标签是否被禁用
<input type="checkbox" name="ifdisabled" value="1" <?php checked(1,get_option('ifdisabled'));?> />
参数详解
checked( $checked, $current = true, $echo = true )
selected( $selected, $current = true, $echo = true )
disabled( $disabled, $current = true, $echo = true )
因为依托同一个函数来实现,所以这三个函数其实接受的参数是一样的,
第一个参数是标准值,
第二个参数是实际值,
第三个函数是是否显示。
即如果第一个值与第二个值相等的话,那三个函数将返回
checked="checked"
selected="selected"
disabled="disabled"
函数源代码
虽然不是每个人都愿意看,但看看总是有点好处的,
/**
* Outputs the html checked attribute.
*
* Compares the first two arguments and if identical marks as checked
*
* @since 1.0.0
*
* @param mixed $checked One of the values to compare
* @param mixed $current (true) The other value to compare if not just true
* @param bool $echo Whether to echo or just return the string
* @return string html attribute or empty string
*/
function checked( $checked, $current = true, $echo = true ) {
return __checked_selected_helper( $checked, $current, $echo, 'checked' );
}
/**
* Outputs the html selected attribute.
*
* Compares the first two arguments and if identical marks as selected
*
* @since 1.0.0
*
* @param mixed $selected One of the values to compare
* @param mixed $current (true) The other value to compare if not just true
* @param bool $echo Whether to echo or just return the string
* @return string html attribute or empty string
*/
function selected( $selected, $current = true, $echo = true ) {
return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
/**
* Outputs the html disabled attribute.
*
* Compares the first two arguments and if identical marks as disabled
*
* @since 3.0.0
*
* @param mixed $disabled One of the values to compare
* @param mixed $current (true) The other value to compare if not just true
* @param bool $echo Whether to echo or just return the string
* @return string html attribute or empty string
*/
function disabled( $disabled, $current = true, $echo = true ) {
return __checked_selected_helper( $disabled, $current, $echo, 'disabled' );
}
/**
* Private helper function for checked, selected, and disabled.
*
* Compares the first two arguments and if identical marks as $type
*
* @since 2.8.0
* @access private
*
* @param any $helper One of the values to compare
* @param any $current (true) The other value to compare if not just true
* @param bool $echo Whether to echo or just return the string
* @param string $type The type of checked|selected|disabled we are doing
* @return string html attribute or empty string
*/
function __checked_selected_helper( $helper, $current, $echo, $type ) {
if ( (string) $helper === (string) $current )
$result = " $type='$type'";
else
$result = '';
if ( $echo )
echo $result;
return $result;
}
总结
本来觉得wordpress不会内置这样的函数,
所以打算自己动手去封装一个checked的,
据我所知,目前很多插件和主题都没有充分利用这三个函数。
结果最近几天在看一些插件的源代码,偶然看到checked函数,
所以就下心思去看了一下,发现这些写wordpress的人真的是高手啊,一个函数解决三个问题。
崇拜中……

