0

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:

media size selector

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');
  • It might just be the copy/paste but I think you're missing a } after the function. Assuming that it's supposed to be insde the setup() function. – Welcher May 1 at 17:12
  • Thanks, that was missing; but that would cause a fatal error, too. – BlueDogRanch May 1 at 17:16
2

You 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' );
1

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.

1

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');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.