Skip to content

Instantly share code, notes, and snippets.

@LucaRosaldi
Created April 6, 2022 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LucaRosaldi/71a1987d3cbbc45e165a767447f09f71 to your computer and use it in GitHub Desktop.
Save LucaRosaldi/71a1987d3cbbc45e165a767447f09f71 to your computer and use it in GitHub Desktop.
WP: add custom image sizes
<?php
/**
* Add custom image sizes.
*
* @hook after_setup_theme
*
* @return void
*/
function my_theme_add_custom_image_sizes() : void
{
add_image_size( 'my-theme-thumbnail', 600, 450, true );
add_image_size( 'my-theme-medium', 900, 675, true );
add_image_size( 'my-theme-large', 1200, 900, true );
}
add_action( 'after_setup_theme', 'my_theme_add_custom_image_sizes' );
/**
* Make custom image sizes selectable
*
* @hook image_size_names_choose
*
* @param array $sizes
* @return array
*/
function my_theme_select_custom_image_sizes( array $sizes ) : array
{
return array_merge( $sizes, [
'my-theme-thumbnail' => __( 'Card Image' ),
'my-theme-medium' => __( 'Section Image' ),
'my-theme-large' => __( 'Background Image' )
]);
}
add_filter( 'image_size_names_choose', 'my_theme_select_custom_image_sizes' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment