Only Your Image Aspect Ratios in the Editor
Replaces the long list of aspect ratios offered for images and featured images with a short set you choose, so cropped images stay consistent.
Opens WPIDE on
Code
// Aspect ratios: slug => [name, ratio].
$wpide_aspect_ratios = array(
'wpide-square' => array( 'Square', '1' ),
'wpide-landscape' => array( 'Landscape 3:2', '3/2' ),
'wpide-wide' => array( 'Wide 16:9', '16/9' ),
);
add_filter( 'wp_theme_json_data_theme', function ( $theme_json ) use ( $wpide_aspect_ratios ) {
$ratios = array();
foreach ( $wpide_aspect_ratios as $slug => $ratio ) {
$ratios[] = array( 'slug' => $slug, 'name' => $ratio[0], 'ratio' => $ratio[1] );
}
return $theme_json->update_with( array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'settings' => array(
'dimensions' => array(
'aspectRatios' => $ratios,
'defaultAspectRatios' => false,
),
),
) );
} );
Notes
Needs WordPress 6.6+. Ratios are written as width/height.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.