Media PHP Beginner

Add Your Own Image Size to the Editor

Register a custom image size, like a 1200×630 social card, and make it selectable in the Image block's Resolution menu.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 9 lines
add_action( 'after_setup_theme', function (): void {
	add_image_size( 'nahnu-wide', 1200, 630, true );
} );

add_filter( 'image_size_names_choose', function ( array $sizes ): array {
	return array_merge( $sizes, [
		'nahnu-wide' => 'Wide (1200×630)',
	] );
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

Theme-registered image sizes are created on upload, but they don’t show up in the editor unless you list them. This snippet does both, so editors can pick your size from the Image block.

How it works

add_image_size() registers a size by name, width and height. The last argument, true, crops images to exactly that shape. Use false to scale them to fit instead.

The image_size_names_choose filter adds the size to the Resolution menu in the Image block and the classic media dialog.

Things to watch

  • Sizes are only generated for new uploads. Use a regenerate thumbnails plugin for existing images.
  • Every extra size adds another file per upload. Only add sizes you’ll use.

How to test it

Upload an image, add it with the Image block and open the Resolution menu. “Wide (1200×630)” should be listed.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only