Using WordPress ‘image_size_input_fields()’ PHP function

The image_size_input_fields WordPress PHP function retrieves HTML for the size radio buttons with the specified one checked.

Usage

image_size_input_fields($post, $check = '');

Parameters

  • $post (WP_Post) – Required, the post object.
  • $check (bool|string) – Optional, the size to be checked. Default: ”.

More information

See WordPress Developer Resources: image_size_input_fields

Examples

Displaying size radio buttons for an image attachment

This example demonstrates how to display size radio buttons for an image attachment.

$post_id = 42; // Replace with your attachment ID
$post = get_post($post_id);
$checked_size = 'medium';

// Display the size radio buttons
echo image_size_input_fields($post, $checked_size);

Displaying size radio buttons with no preselected size

This example demonstrates how to display size radio buttons without preselecting any size.

$post_id = 42; // Replace with your attachment ID
$post = get_post($post_id);

// Display the size radio buttons without preselecting any size
echo image_size_input_fields($post);

Using image_size_input_fields in a custom form

This example demonstrates how to use image_size_input_fields in a custom form for the user to select an image size.

// Custom form
echo '<form action="process_image.php" method="post">';
$post_id = 42; // Replace with your attachment ID
$post = get_post($post_id);
$checked_size = 'medium';

// Display the size radio buttons
echo image_size_input_fields($post, $checked_size);

echo '<input type="submit" value="Submit">';
echo '</form>';

Displaying size radio buttons for a custom set of image sizes

This example demonstrates how to display size radio buttons for a custom set of image sizes.

function custom_image_size_input_fields($post, $check = '') {
    $image_sizes = array('thumbnail', 'custom_size', 'large');
    return image_size_input_fields($post, $check, $image_sizes);
}

$post_id = 42; // Replace with your attachment ID
$post = get_post($post_id);

// Display the size radio buttons for the custom set of image sizes
echo custom_image_size_input_fields($post, 'custom_size');

Displaying size radio buttons and setting a default size

This example demonstrates how to display size radio buttons and set a default size if none is provided.

$post_id = 42; // Replace with your attachment ID
$post = get_post($post_id);
$checked_size = get_option('image_default_size', 'medium');

// Display the size radio buttons with the default size selected
echo image_size_input_fields($post, $checked_size);