disabled()
云策文档标注
概述
disabled() 函数用于输出 HTML 的 disabled 属性,通过比较两个参数值来决定是否标记为禁用状态。它基于 __checked_selected_helper() 辅助函数实现,常用于表单元素的条件禁用。
关键要点
- 函数比较 $disabled 和 $current 参数,若相同则输出 disabled 属性字符串。
- 参数 $display 控制是否直接回显或返回字符串,默认回显。
- 函数返回 HTML 属性字符串或空字符串,适用于表单字段的禁用逻辑。
- 与 checked() 和 selected() 函数类似,共享 __checked_selected_helper() 辅助函数。
- 在 WordPress 3.0.0 版本引入,被多个核心函数如 wp_popular_terms_checklist() 和 post_tags_meta_box() 使用。
代码示例
function disabled( $disabled, $current = true, $display = true ) {
return __checked_selected_helper( $disabled, $current, $display, 'disabled' );
}注意事项
- 确保 $disabled 和 $current 参数类型匹配,以避免意外行为。
- 在自定义表单或插件开发中,可结合条件逻辑调用此函数来动态控制元素禁用状态。
原文内容
Outputs the HTML disabled attribute.
Description
Compares the first two arguments and if identical marks as disabled.
Parameters
$disabledmixedrequired-
One of the values to compare.
$currentmixedoptional-
The other value to compare if not just true.
Default:
true $displaybooloptional-
Whether to echo or just return the string.
Default:
true
Source
function disabled( $disabled, $current = true, $display = true ) {
return __checked_selected_helper( $disabled, $current, $display, 'disabled' );
}
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |
Skip to note 2 content
Codex
Enable/disable text field according to the given value.
<input type="text" <?php disabled( $disable_text_field, true ); ?> name="sample_text_field" />