I'm using the example from add_image_size() | WordPress Developer Resources to try to add custom image sizes to the admin media selector.
add_filter( 'image_size_names_choose', 'my_custom_sizes' );
function my_custom_sizes( $sizes ) {
return array_merge( $sizes, array(
'your-custom-size' => __( 'Your Custom Size Name' ),
) );
}
But I'm getting a PHP Warning:
call_user_func_array() expects parameter 1 to be a valid callback, function 'my_custom_sizes' not found or invalid function name
and the media size selector is empty:
The answers to Adding custom image size to the media image editor and add_image_size and add_filter('image_size_names_choose', 'my_custom_image_sizes') not working with wordpress 3.5.2 don't work.
I'm using the filter and add_image_size
inside after_setup_theme
; see below. I've tried it outside with no luck. Thumbnails are being generated, and regenerating thumbs works, too.
What is breaking?
function setup()
{
// other code
add_image_size( 'banner-ad', 655, 100, true );
add_image_size( 'banner-rectangle', 655, 250, true );
add_filter( 'image_size_names_choose', 'my_custom_sizes' );
function my_custom_sizes( $sizes ) {
return array_merge( $sizes, array(
'banner-ad' => __( 'Banner Ad' ),
'banner-rectangle' => __( 'Banner Rectangle' ),
) );
}
// other code
}
add_action('after_setup_theme', __NAMESPACE__ . '\\setup');
I'm using the example from add_image_size() | WordPress Developer Resources to try to add custom image sizes to the admin media selector.
add_filter( 'image_size_names_choose', 'my_custom_sizes' );
function my_custom_sizes( $sizes ) {
return array_merge( $sizes, array(
'your-custom-size' => __( 'Your Custom Size Name' ),
) );
}
But I'm getting a PHP Warning:
call_user_func_array() expects parameter 1 to be a valid callback, function 'my_custom_sizes' not found or invalid function name
and the media size selector is empty:
The answers to Adding custom image size to the media image editor and add_image_size and add_filter('image_size_names_choose', 'my_custom_image_sizes') not working with wordpress 3.5.2 don't work.
I'm using the filter and add_image_size
inside after_setup_theme
; see below. I've tried it outside with no luck. Thumbnails are being generated, and regenerating thumbs works, too.
What is breaking?
function setup()
{
// other code
add_image_size( 'banner-ad', 655, 100, true );
add_image_size( 'banner-rectangle', 655, 250, true );
add_filter( 'image_size_names_choose', 'my_custom_sizes' );
function my_custom_sizes( $sizes ) {
return array_merge( $sizes, array(
'banner-ad' => __( 'Banner Ad' ),
'banner-rectangle' => __( 'Banner Rectangle' ),
) );
}
// other code
}
add_action('after_setup_theme', __NAMESPACE__ . '\\setup');
Share
Improve this question
edited May 1, 2019 at 17:16
BlueDogRanch
asked May 1, 2019 at 17:10
BlueDogRanchBlueDogRanch
1705 silver badges25 bronze badges
2
|
3 Answers
Reset to default 2You can't define a hook callback function inside another function.
You've defined my_custom_sizes()
inside setup()
, which means that my_custom_sizes()
cannot be called outside of the scope of setup()
. This is a problem because the hook, image_size_names_choose
, is not run inside your setup()
function. You need to define your callback in the global scope:
function setup()
{
add_image_size( 'banner-ad', 655, 100, true );
add_image_size( 'banner-rectangle', 655, 250, true );
}
add_action('after_setup_theme', __NAMESPACE__ . '\\setup');
function my_custom_sizes( $sizes )
{
return array_merge( $sizes, array(
'banner-ad' => __( 'Banner Ad' ),
'banner-rectangle' => __( 'Banner Rectangle' ),
) );
}
add_filter( 'image_size_names_choose', __NAMESPACE__ . '\\my_custom_sizes' );
Try defining the my_custom_sizes()
function outside of the setup()
function.
I'm pretty sure that the my_custom_sizes()
function is only available in the scope of the setup()
function - which has already been called before the image_size_names_choose
filter is fired and so it's not available to it.
Try this
function my_custom_sizes( $sizes ) {
return array_merge( $sizes, array(
'banner-ad' => __( 'Banner Ad' ),
'banner-rectangle' => __( 'Banner Rectangle' ),
) );
}
add_filter( 'image_size_names_choose', 'my_custom_sizes' );
function setup()
{
// other code
add_image_size( 'banner-ad', 655, 100, true );
add_image_size( 'banner-rectangle', 655, 250, true );
// other code
}
add_action('after_setup_theme', __NAMESPACE__ . '\\setup');
}
after the function. Assuming that it's supposed to be insde thesetup()
function. – Welcher Commented May 1, 2019 at 17:12