Gallery Settings Change available Columns

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"
        data-setting="columns">
        <?php for ( $i = 1; $i <= 9; $i++ ) : ?>
            <option value="<?php echo esc_attr( $i ); ?>" <?php selected( $i, 3 ); ?>>
                <?php echo esc_html( $i ); ?>
            </option>
        <?php endfor; ?>
    </select>
</label>

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

Read More

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

Eugene

Related posts

Leave a Reply

2 comments

  1. 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.

    <?php
    /* 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;
        $static++;
    
        // 
        if ( 1 === $static )
        {
            ob_start();
            return $safe_text;
        }
        if ( in_array( $static, array( 7, 9 ) ) )
        {
            remove_filter( current_filter(), __FUNCTION__ );
            if ( 'esc_html' === current_filter() )
            {
                $html = ob_get_contents();
                ob_end_clean();
                // 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.

    EDIT

    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).

  2. 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.)

    Style

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

    Function

    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

    Notes

    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.