函数文档

image_size_input_fields()

💡 云策文档标注

概述

image_size_input_fields() 函数用于生成图像尺寸选择器的 HTML 代码,包括单选按钮和标签,并支持指定默认选中项。它返回一个数组,包含标签、输入类型和 HTML 内容,适用于 WordPress 媒体编辑界面。

关键要点

  • 函数接受两个参数:$post(WP_Post 对象,必需)和 $check(布尔或字符串,必需),用于指定默认选中的图像尺寸。
  • 通过 image_size_names_choose 过滤器可以自定义图像尺寸的名称和标签,默认包括缩略图、中等、大和全尺寸。
  • 函数内部使用 image_downsize() 检查每个尺寸是否可用,并根据 $check 参数和可用性动态设置选中状态。
  • 返回数组结构为:'label' 是翻译后的“Size”标签,'input' 固定为 'html','html' 是生成的单选按钮 HTML 字符串。

代码示例

function image_size_input_fields( $post, $check = '' ) {
    $size_names = apply_filters(
        'image_size_names_choose',
        array(
            'thumbnail' => __( 'Thumbnail' ),
            'medium'    => __( 'Medium' ),
            'large'     => __( 'Large' ),
            'full'      => __( 'Full Size' ),
        )
    );
    if ( empty( $check ) ) {
        $check = get_user_setting( 'imgsize', 'medium' );
    }
    $output = array();
    foreach ( $size_names as $size => $label ) {
        $downsize = image_downsize( $post->ID, $size );
        $enabled = ( $downsize[3] || 'full' === $size );
        $css_id = "image-size-{$size}-{$post->ID}";
        $checked = '';
        if ( $size === $check ) {
            if ( $enabled ) {
                $checked = " checked='checked'";
            } else {
                $check = '';
            }
        } elseif ( ! $check && $enabled && 'thumbnail' !== $size ) {
            $check = $size;
            $checked = " checked='checked'";
        }
        $html = "<input type='radio' name='attachments[{$post->ID}][image-size]' id='{$css_id}' value='{$size}'$checked />";
        $html .= "<label for='{$css_id}'>$label</label>";
        if ( $enabled ) {
            $html .= " " . sprintf( '(%d × %d)', $downsize[1], $downsize[2] ) . "";
        }
        $html .= '<br />';
        $output[] = $html;
    }
    return array(
        'label' => __( 'Size' ),
        'input' => 'html',
        'html'  => implode( "n", $output ),
    );
}

注意事项

  • 函数依赖于 image_downsize() 来获取图像尺寸的可用性和维度,确保图像处理功能正常。
  • 通过 get_user_setting() 获取用户设置,如果没有指定 $check 参数,则默认使用 'medium' 尺寸。
  • 生成的 HTML 包括单选按钮、标签和尺寸信息(如果可用),适用于表单集成。

📄 原文内容

Retrieves HTML for the size radio buttons with the specified one checked.

Parameters

$postWP_Postrequired
$checkbool|stringrequired

Return

array

Source

function image_size_input_fields( $post, $check = '' ) {
	/**
	 * Filters the names and labels of the default image sizes.
	 *
	 * @since 3.3.0
	 *
	 * @param string[] $size_names Array of image size labels keyed by their name. Default values
	 *                             include 'Thumbnail', 'Medium', 'Large', and 'Full Size'.
	 */
	$size_names = apply_filters(
		'image_size_names_choose',
		array(
			'thumbnail' => __( 'Thumbnail' ),
			'medium'    => __( 'Medium' ),
			'large'     => __( 'Large' ),
			'full'      => __( 'Full Size' ),
		)
	);

	if ( empty( $check ) ) {
		$check = get_user_setting( 'imgsize', 'medium' );
	}

	$output = array();

	foreach ( $size_names as $size => $label ) {
		$downsize = image_downsize( $post->ID, $size );
		$checked  = '';

		// Is this size selectable?
		$enabled = ( $downsize[3] || 'full' === $size );
		$css_id  = "image-size-{$size}-{$post->ID}";

		// If this size is the default but that's not available, don't select it.
		if ( $size === $check ) {
			if ( $enabled ) {
				$checked = " checked='checked'";
			} else {
				$check = '';
			}
		} elseif ( ! $check && $enabled && 'thumbnail' !== $size ) {
			/*
			 * If $check is not enabled, default to the first available size
			 * that's bigger than a thumbnail.
			 */
			$check   = $size;
			$checked = " checked='checked'";
		}

		$html = "<div class='image-size-item'><input type='radio' " . disabled( $enabled, false, false ) . "name='attachments[$post->ID][image-size]' id='{$css_id}' value='{$size}'$checked />";

		$html .= "<label for='{$css_id}'>$label</label>";

		// Only show the dimensions if that choice is available.
		if ( $enabled ) {
			$html .= " <label for='{$css_id}' class='help'>" . sprintf( '(%d × %d)', $downsize[1], $downsize[2] ) . '</label>';
		}
		$html .= '</div>';

		$output[] = $html;
	}

	return array(
		'label' => __( 'Size' ),
		'input' => 'html',
		'html'  => implode( "n", $output ),
	);
}

Hooks

apply_filters( ‘image_size_names_choose’, string[] $size_names )

Filters the names and labels of the default image sizes.

Changelog

Version Description
2.7.0 Introduced.