changing the wordpress gallery image size default

I was wondering if any could help on my problem

I have this code from someone that runs fine except that the size does not function, default is always on “thumbnail”

Read More
function my_gallery_default_type_set_link( $settings ) {
    $settings['galleryDefaults']['link'] = 'file';
    $settings['galleryDefaults']['columns'] = '4';
    $settings['galleryDefaults']['size'] = 'large';
    return $settings;
}
add_filter( 'media_view_settings', 'my_gallery_default_type_set_link');

how can I make this always in large as a default?

Related posts

3 comments

  1. This piece of code is actually working, the size of the gallery will be “large” by default if an other size is not manually selected. The real problem come from the dropdown itself that is not correctly set on initialisation, still in WP 4.8.2.

    There is a ticket open with more details about this display error.

    In the meantime, I found a workaround using the print_media_templates hook :

    Step 1 – Define your gallery default image size

    function my_gallery_default_settings( $settings ) {
        $settings['galleryDefaults']['size'] = 'large';
        return $settings;
    }
    add_filter( 'media_view_settings', 'my_gallery_default_settings');
    

    Step 2 – Debug the dropdown image size default value

    function debug_gallery_image_size_default_value() {
      ?>
    
      <script>
        jQuery(document).ready(function(){
          wp.media.view.Settings.Gallery = wp.media.view.Settings.Gallery.extend({
            template: function(view){
              var base_view = wp.media.template('gallery-settings')(view);
              var size_option_search = '<option value="'+wp.media.gallery.defaults.size+'">';
              var size_option_replace = '<option value="'+wp.media.gallery.defaults.size+'" selected="selected">';
              base_view = base_view.replace(size_option_search, size_option_replace);
              return base_view;
            }
          });
        });
      </script>
    
      <?php
    }
    add_action('print_media_templates', 'debug_gallery_image_size_default_value');
    
  2. Actually, other code in other answers replaces default settings for existing galleries. Here’s the code to apply default settings only to the new gallery:

    add_filter( 'media_view_settings', 'theme_gallery_defaults', 10, 2 );
    
    function theme_gallery_defaults( $settings, $post ) {
        $defaults = ! empty( $settings['galleryDefaults'] ) && is_array( $settings['galleryDefaults'] ) ? $settings['galleryDefaults'] : array();
        $settings['galleryDefaults'] = array_merge( $defaults, array(
            'columns'   => 5,
            'size'      => 'large',
            'link'      => 'file'
        ) );
        return $settings;
    }
    

Comments are closed.