函数文档

__checked_selected_helper()

💡 云策文档标注

概述

__checked_selected_helper() 是 WordPress 中的一个私有辅助函数,用于生成 HTML 属性如 checked、selected、disabled 和 readonly。它通过比较两个值,如果相同则输出相应的属性字符串。

关键要点

  • 函数用于比较两个值,如果相等则生成指定类型的 HTML 属性(如 checked='checked')。
  • 参数包括 $helper(比较值)、$current(当前值)、$display(是否直接输出)和 $type(属性类型)。
  • 返回 HTML 属性字符串或空字符串,支持通过 $display 参数控制输出方式。

代码示例

function __checked_selected_helper( $helper, $current, $display, $type ) {
    if ( (string) $helper === (string) $current ) {
        $result = " $type='$type'";
    } else {
        $result = '';
    }

    if ( $display ) {
        echo $result;
    }

    return $result;
}

注意事项

  • 这是一个私有函数,通常不应直接调用,而是通过 checked()、selected()、disabled() 或 wp_readonly() 等公共函数使用。
  • 函数名以双下划线开头,遵循 WordPress 命名约定,表示内部使用。
  • 参数 $helper 和 $current 在比较前会被转换为字符串,确保类型安全。

📄 原文内容

Private helper function for checked, selected, disabled and readonly.

Description

Compares the first two arguments and if identical marks as $type.

Parameters

$helpermixedrequired
One of the values to compare.
$currentmixedrequired
The other value to compare if not just true.
$displayboolrequired
Whether to echo or just return the string.
$typestringrequired
The type of checked|selected|disabled|readonly we are doing.

Return

string HTML attribute or empty string.

Source

function __checked_selected_helper( $helper, $current, $display, $type ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore
	if ( (string) $helper === (string) $current ) {
		$result = " $type='$type'";
	} else {
		$result = '';
	}

	if ( $display ) {
		echo $result;
	}

	return $result;
}

Changelog

Version Description
2.8.0 Introduced.