WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#28693 closed enhancement (fixed)

Make gallery defaults configurable

Reported by: michal429 Owned by: wonderboymusic
Milestone: 4.0 Priority: normal
Severity: normal Version: 4.0
Component: Gallery Keywords: has-patch
Focuses: javascript Cc:
PR Number:

Description

Please make the "new gallery" default options configurable. Right now there are hard-coded defaults of 3 columns and link to post. All galleries on my sites are 5-column wide and link to files with colorbox slideshow. Right now I have to override the whole gallery drawing just for that and tell users to ignore the settings when creating a gallery.

Attachments (2)

28693.diff (1.8 KB) - added by adamsilverstein 5 years ago.
make gallery defaults configurable
28693.2.diff (1.1 KB) - added by wonderboymusic 5 years ago.

Download all attachments as: .zip

Change History (22)

#1 @iseulde
5 years ago

  • Component changed from Administration to Gallery
  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #22773.

@adamsilverstein
5 years ago

make gallery defaults configurable

#2 @adamsilverstein
5 years ago

  • Keywords has-patch dev-feedback added

michal429 -

Seems like what you need here is a way to filter the default values - which are currently hard-coded into the media-editor.js JavaScript. I can certainly see the use case for this!

I am attempting this in 28693.diff, moved default values over to wp.media.view.settings.galleryDefaults which you can adjust using the existing media_view_settings filter. For example, to set the default value gallery columns you would do something like this:

function my_set_gallery_default_five_columns( $settings ) {
	$settings['galleryDefaults']['columns'] = '5';
	return $settings;
}
add_filter( 'media_view_settings', 'my_set_gallery_default_five_columns', 10 );

#3 @adamsilverstein
5 years ago

  • Resolution duplicate deleted
  • Status changed from closed to reopened

#4 @GregLone
5 years ago

Hi.

I agree. Not being able to change these default settings is very annoying.

I think there's more things to change:
wp-admin/js/gallery.js L215

if ( I('linkto-file').checked ) {
	s += ' link="file"';
	setUserSetting('galfile', '1');
}

if ( I('order-desc').checked ) {
	s += ' order="DESC"';
	setUserSetting('galdesc', '1');
}

if ( I('columns').value !== 3 ) {
	s += ' columns="' + I('columns').value + '"';
	setUserSetting('galcols', I('columns').value);
}

wp-includes/js/mce-views.js L316:

options = {
	attachments: attachments,
	columns: attrs.columns ? parseInt( attrs.columns, 10 ) : 3
};
Version 0, edited 5 years ago by GregLone (next)

#5 @wonderboymusic
5 years ago

  • Focuses javascript added
  • Keywords dev-feedback removed
  • Milestone set to 4.0

28693.2.diff does this without us having to localize

#6 follow-up: @wonderboymusic
5 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from reopened to closed

In 28990:

Allow users to override default gallery settings in JS via wp_localize_script(). This is already possible in PHP by filtering shortcode_atts_gallery.

Fixes #28693.

#7 @wonderboymusic
5 years ago

In 28991:

The MCE view for galleries needs to read wp.media.galleryDefaults.columns after [28990].

See #28693.

#8 in reply to: ↑ 6 @adamsilverstein
5 years ago

Looks good, nice work!

Replying to wonderboymusic:

In 28990:

Allow users to override default gallery settings in JS via wp_localize_script(). This is already possible in PHP by filtering shortcode_atts_gallery.

Fixes #28693.

#9 follow-up: @GregLone
5 years ago

  • Keywords needs-testing added
  • Resolution fixed deleted
  • Status changed from closed to reopened

Hi.

It seems it doesn't work properly. Here what I've done:

add_filter( 'media_view_settings', 'sfgv_localize_gallery_defaults', 10, 2 );

function sfgv_localize_gallery_defaults( $settings, $post ) {
	$defaults = ! empty( $settings['galleryDefaults'] ) && is_array( $settings['galleryDefaults'] ) ? $settings['galleryDefaults'] : array();
	$settings['galleryDefaults'] = array_merge( $defaults, array(
		'itemtag'	=> 'figure',
		'icontag'	=> 'div',
		'captiontag'	=> 'figcaption',
		'columns'	=> 5,
		'size'		=> 'gallery-thumb',
		'link'		=> 'file',
	) );
	return $settings;
}

Results:

The custom values are added to the _wpMediaViewsL10n JS var, good.

In the gallery creation window:

  • The default columns value is 3,
  • The default link value is post.

In short, none of my custom values are applied.

I keep the default values as it and send the gallery to the editor:

  • I have 5 columns,
  • The tags are dl/dt/dd,
  • The image size is still thumbnail.

In short, only the number of columns is applied.

I click the gallery, then the edit button:

  • Still 3 columns and "post".

One more thing.
If html5 support is set for the gallery shortcode, the tags used are figure/div/figcaption instead of dl/dt/dd. Perhaps should we do the same things in the editor? But it may not be really useful though.

#10 in reply to: ↑ 9 ; follow-up: @adamsilverstein
5 years ago

  • Resolution set to worksforme
  • Status changed from reopened to closed

I don't think my proposed filter is needed and it will not be included in core, sorry for any confusion this created. The resolution took a different approach:

You can override the settings in JavaScript by setting wp.media.view.settings.galleryDefaults with what you want (in trunk after wonderboymusic's patch above)

You can change the defaults from PHP by filtering shortcode_atts_gallery - this code sample shows how - https://gist.github.com/adamsilverstein/cd2675feb7233e4849b7

Hope that makes sense and works to resolve your issue.

Replying to GregLone:

Hi.

It seems it doesn't work properly. Here what I've done:

add_filter( 'media_view_settings', 'sfgv_localize_gallery_defaults', 10, 2 );

function sfgv_localize_gallery_defaults( $settings, $post ) {
	$defaults = ! empty( $settings['galleryDefaults'] ) && is_array( $settings['galleryDefaults'] ) ? $settings['galleryDefaults'] : array();
	$settings['galleryDefaults'] = array_merge( $defaults, array(
		'itemtag'	=> 'figure',
		'icontag'	=> 'div',
		'captiontag'	=> 'figcaption',
		'columns'	=> 5,
		'size'		=> 'gallery-thumb',
		'link'		=> 'file',
	) );
	return $settings;
}

Results:

The custom values are added to the _wpMediaViewsL10n JS var, good.

In the gallery creation window:

  • The default columns value is 3,
  • The default link value is post.

In short, none of my custom values are applied.

I keep the default values as it and send the gallery to the editor:

  • I have 5 columns,
  • The tags are dl/dt/dd,
  • The image size is still thumbnail.

In short, only the number of columns is applied.

I click the gallery, then the edit button:

  • Still 3 columns and "post".

One more thing.
If html5 support is set for the gallery shortcode, the tags used are figure/div/figcaption instead of dl/dt/dd. Perhaps should we do the same things in the editor? But it may not be really useful though.

#11 in reply to: ↑ 10 ; follow-up: @GregLone
5 years ago

  • Resolution worksforme deleted
  • Status changed from closed to reopened

Replying to adamsilverstein:

I don't think my proposed filter is needed and it will not be included in core, sorry for any confusion this created. The resolution took a different approach:

You can override the settings in JavaScript by setting wp.media.view.settings.galleryDefaults with what you want (in trunk after wonderboymusic's patch above)

Indeed, and that's what I'm doing with the filter: $settings['galleryDefaults'] will populate the JS variable _wpMediaViewsL10n, and then will set wp.media.view.settings.galleryDefaults. So, my custom values are in wp.media.view.settings.galleryDefaults. The problem is that values alone do (almost) nothing.

You can change the defaults from PHP by filtering shortcode_atts_gallery - this code sample shows how - https://gist.github.com/adamsilverstein/cd2675feb7233e4849b7

Yep, I know.

Hope that makes sense and works to resolve your issue.

#12 in reply to: ↑ 11 ; follow-up: @adamsilverstein
5 years ago

Why are you re-opening the ticket, do you still have an issue?

Replying to GregLone:

Replying to adamsilverstein:

I don't think my proposed filter is needed and it will not be included in core, sorry for any confusion this created. The resolution took a different approach:

You can override the settings in JavaScript by setting wp.media.view.settings.galleryDefaults with what you want (in trunk after wonderboymusic's patch above)

Indeed, and that's what I'm doing with the filter: $settings['galleryDefaults'] will populate the JS variable _wpMediaViewsL10n, and then will set wp.media.view.settings.galleryDefaults. So, my custom values are in wp.media.view.settings.galleryDefaults. The problem is that values alone do (almost) nothing.

You can change the defaults from PHP by filtering shortcode_atts_gallery - this code sample shows how - https://gist.github.com/adamsilverstein/cd2675feb7233e4849b7

Yep, I know.

Hope that makes sense and works to resolve your issue.

#13 in reply to: ↑ 12 ; follow-up: @GregLone
5 years ago

Replying to adamsilverstein:

Why are you re-opening the ticket, do you still have an issue?

Unless something changed since my latest post, yes, it won't work. Have you tried to have custom parameters in the editor and the media modal?

#14 in reply to: ↑ 13 @adamsilverstein
5 years ago

Replying to GregLone:

Replying to adamsilverstein:

Why are you re-opening the ticket, do you still have an issue?

Unless something changed since my latest post, yes, it won't work. Have you tried to have custom parameters in the editor and the media modal?

i will give it a try to see why its not working.

#15 follow-up: @lilqhgal
5 years ago

Sorry for butting in here but I'm curious. Will this change allow some customization from the Media Gallery modal itself? Ideally, I'd like to be able to add some filters or something to my functions file and then the user is displayed with some options during Gallery creation. Instead of just the "Link to" and "Column sizes" that are currently available, I'd like to also see a dropdown for "image size", which you could even go further to add the custom sizes from your functions in there. This would also help theme developers out. Thank you!

#16 @wonderboymusic
5 years ago

In 29283:

The Underscore templates for Gallery settings should respect defaults when marking an <option> as selected.

See #28693.

#17 @wonderboymusic
5 years ago

In 29284:

When gallery settings are overridden, the JS-generated shortcodes need to check the new defaults before deleting attributes that it suspects are the same as the original default values.

wp.media.collection has a new method to do this: setDefaults(). Also flips the use of _.extend to allow this method to be overriden on instance creation.

See #28693.

#18 @wonderboymusic
5 years ago

  • Keywords needs-testing removed
  • Resolution set to fixed
  • Status changed from reopened to closed

Changing the markup in the MCE view for HTML5 will be super gnarly in the Underscore template. I fixed the main issues you pointed out.

#19 in reply to: ↑ 15 @adamsilverstein
5 years ago

lilqhgal,

I don't think this ticket will help you - its just about overriding the defaults for the existing values. There may already be action on this in another ticket or a good approach to use now; Otherwise, consider opening a new ticket to request this feature.

Replying to lilqhgal:

Sorry for butting in here but I'm curious. Will this change allow some customization from the Media Gallery modal itself? Ideally, I'd like to be able to add some filters or something to my functions file and then the user is displayed with some options during Gallery creation. Instead of just the "Link to" and "Column sizes" that are currently available, I'd like to also see a dropdown for "image size", which you could even go further to add the custom sizes from your functions in there. This would also help theme developers out. Thank you!

#20 @GregLone
5 years ago

Great.

It seems to work fine so far: when I want to create a new gallery, the number of columns and the link are preset with the custom values.
Unfortunately I couldn't see if these custom values are used when Editing an existing gallery: the "Edit" and "Remove" buttons in the editor don't work anymore (related?).

A bit further: to be able to also have a different image size in the galleries, should I open a new ticket for this enhancement?

Thanks.

Note: See TracTickets for help on using tickets.