Is there a filter hook which changes the number of available columns in gallery settings screen. I'm talking about the :

<label class="setting">
    <span><?php _e('Columns'); ?></span>
    <select class="columns" name="columns"
        <?php for ( $i = 1; $i <= 9; $i++ ) : ?>
            <option value="<?php echo esc_attr( $i ); ?>" <?php selected( $i, 3 ); ?>>
                <?php echo esc_html( $i ); ?>
        <?php endfor; ?>

in wp-includes/media-template.php line 359-369 (Wordpress v3.5)

What I basically want to do is to change $i <= 9; to $i <= 5; in loop exit condition.

I guess if I just do so, it will be discarded with the next Wordpress update.

Thanks for your help


2 Answers 2


Short answer

Simple as things sometimes are: No this is not possible. The whole part is hard coded.

Long answer (not recommended to do so)

You could maybe jump into the esc_html and attribute_escape filters and just return empty there *), but as those are pretty generic names and would possibly also interfere with other filter results and therefore break other things.

/* Plugin Name: (#82379) Empty Media options */
add_filter( 'image_size_names_choose', 'wpse82379_esc_attr' );
function wpse82379_trigger_filter( $sizes )
    if ( 'post' !== get_current_screen()->post )
        return $safe_text;

    add_filter( 'attribute_escape', 'wpse82379_esc_attr', 10, 2 );
    add_filter( 'esc_html', 'wpse82379_esc_attr', 10, 2 );

    return $sizes;
function wpse82379_esc_attr( $safe_text, $text )
    if ( ! in_array( absint( $text ), range( 6, 9 ) ) )
        return $safe_text;

    static $counter = 0;

    if ( 1 === $static )
        return $safe_text;
    if ( in_array( $static, array( 7, 9 ) ) )
        remove_filter( current_filter(), __FUNCTION__ );
        if ( 'esc_html' === current_filter() )
            $html = ob_get_contents();
            // In case you want to alter the layout, rework $html and return it
            return '';

    return $safe_text;

As the escape-filters are present nearly everywhere, it's not really recommended to use them in production (like with the gettext-filters). They trigger on every escape (which are hundreds per page) and can really slow down your system.

Internals and performance

To work against this, I've used the filter that runs last before the HTML script output to add the callbacks as late as possible. Then I abort as often as possible to not trigger this plugins filters on any other page than the post "add new" screen. After checking the source files HTML, I can see that with version 3.5 we only have two calls for esc_attr_e()(which internally triggers esc_attr and therefore our filters), but with two strings, so we can safely abort on those. When we finally reach our filters I return an empty string and then instantly remove the filter to not trigger for any later calls.

This is the best you can achieve.


After thinking about it, I used ob caching - not beautiful, but it should work (unless I counted wrong). Note: This is coded while writing and thinking, so it's not tested. You'll have to confirm if it works or not.

Final note: You'll have to recheck this plugin after each following version (starting with WP v3.6) to make sure the markup hasn't changed and it's still working as expected.

*) This would leave you with empty strings. In other words: They would still be there but non-functional and empty (no string).

  • Final note: You'll have to recheck this plugin after each following version (starting with WP v3.6) to make sure the markup hasn't changed and it's still working as expected.
    – kaiser
    Jan 20, 2013 at 15:32
  • Hey Kaiser. Thanks for your reply! Unfortunately the code doesn't work. I'm getting Parse error: syntax error, unexpected 'return' (T_RETURN)... on line 17 in the fist return of function wpse82379_esc_attr. The line return $safe_text;
    – eugene.kud
    Jan 20, 2013 at 15:42
  • 1
    @eugene.kud Yeah, a missing bracket - fixed. Btw: You should try to dig a little bit into error messages, as those are pretty clear and start being help instead of rode blockers when you got around the 5 basic or so messages. Another hint: Use a real IDE as those highlight such errors. I found it with the IDE pretty fast (without counting lines in code in here).
    – kaiser
    Jan 20, 2013 at 15:55

There may or may not be a filter hook, but you can restrict the number of available columns in gallery settings screen by using styles - specifically the admin CSS.

To achieve choices of 1-5 columns add this to your WordPress admin stylesheet. (You may need to create one.)


.gallery-settings .columns option:nth-child(n+6) {
    display: none;


Tell WordPress to pull this in using wp_enqueue_style in your enqueue PHP file.

function wp_admin_styles() {
    wp_enqueue_style('admin-styles', get_template_directory_uri().'/wordpress-admin.css');
add_action('admin_enqueue_scripts', 'wp_admin_styles');

enter image description here


If the <select> <option> tag(s) isn't there it cannot be chosen.* In the example above all the options starting with the 6th child are targeted. Notice this will be restricted to only the .columns class in a .gallery-settings class, so it's pretty safe to say this probably won't cause any problems with other admin select options.

For my project I targeted only the 9th option with nth-child(9). That's because I'm using Zurb Foundation's block-grid with choices of 1-8 columns. Choosing 9 just breaks the layout.

With this sitting in a stylesheet there's no need to worry about losing anything after an update.

*The choice will be limited but keep in mind that the user may still be able to set the column number using the text editor option. [gallery columns="9" ids="113,107,92,57,45,44"] But if you are smart you've probably turned off the text editor option for users anyway.

  • Here's what I used to hide everything but 3, 4, 5, 6: .gallery-settings .columns option:nth-child(-n+2), .gallery-settings .columns option:nth-child(n+7) {display: none;} Jan 26, 2019 at 20:40

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.