Make WordPress Core

Opened 14 years ago

Closed 12 years ago

Last modified 12 years ago

#15155 closed enhancement (fixed)

Allow filtering of shortcode attributes

Reported by: coffee2code's profile coffee2code Owned by: nacin's profile nacin
Milestone: 3.6 Priority: normal
Severity: normal Version: 2.5
Component: Shortcodes Keywords: has-patch 3.5-early commit needs-codex
Focuses: Cc:

Description (last modified by DrewAPicture)

It would be handy to have the ability to filter the array returned from shortcode_atts(). This would allow plugins to take the default shortcode attributes, the user supplied attributes, and the merged array of those two into account prior to shortcode_atts() returning.

Potential uses:

  • Override the default shortcode attributes (in a fashion)
  • Ignore certain attribute values from the user and instead use the defaults
  • Dynamically generate shortcode attribute values
  • Modifying the behavior of a shortcode handler via attributes without redefining and rewriting the handler, particularly if the shortcode handler doesn't present filters of its own

This would be particularly useful to modify the behavior of core-supplied shortcodes. For instance, #14390 requests a way of omitting post thumbnails from the gallery shortcode. Using this patch, this could easily be done via hooks in a number of ways (i.e. omit post thumbnails if the 'exclude' attribute isn't supplied, or always ignore post thumbnails, etc). In fact, these hooks allow for custom exclusion of any number of images for any number of custom reasons.

The patch introduces two filters, "shortcode_atts_$shortcode" and "shortcode_atts". The former allows hooking only the shortcode_atts for a specified shortcode, whereas the latter is a more generic hook.

shortcode_atts() really has no context, so I introduced an optional argument, $shortcode. If supplied, the new hooks are fired. I opted not to fire them if no $shortcode was supplied since the lack of context (namely, the name of the shortcode whose attributes are being processed) makes the use of the hooks a questionable proposition. The patch amends all existing shortcode_atts() calls to include the new argument. We'd want to promote use of the argument going forward, which is why I (perhaps overzealously) made use of _deprecated_argument() to alert developers not to a deprecated argument, but to an added argument (is there a better way? making the argument required would of course break a lot plugins).

By way of example, here's a patch for #14390 making use of this patch to omit the post thumbnail from the gallery shortcode:

add_filter( 'shortcode_atts_gallery', 'gallery_shortcode_exclude_thumbnail', 10, 3 );

/**
 * Exclude post thumbnail image from gallery if user doesn't explicitly exclude any images
 *
 * @since 3.1
 * @param array $result The shortcode_atts() merging of $defaults and $atts
 * @param array $defaults The default attributes defined for the shortcode
 * @param array $atts The attributes supplied by the user within the shortcode
 * @return array The potentially modified $result array
 *
 */
function gallery_shortcode_exclude_thumbnail( $result, $defaults, $atts ) {
	if ( empty( $atts['exclude'] ) )
		$result['exclude'] = get_post_thumbnail_id();
	return $result;
}

Another example would be modifying the [caption] shortcode to prepend or append text to whatever caption the user provided.

function append_copyright_to_caption( $result, $defaults, $atts ) {
	$copyright = '© 2010 Example, Inc.';
	if ( isset( $result['caption'] )  && ! empty( $result['caption'] ) )
		$result['caption'] .= "<br />$copyright";
	else
		$result['caption'] =  $copyright;
	return $result;
}
add_filter( 'shortcode_atts_caption', 'append_copyright_to_caption', 10, 3);

Attachments (6)

15155.diff (1.7 KB) - added by coffee2code 14 years ago.
Patch mentioned in ticket.
15155.2.diff (1.4 KB) - added by coffee2code 13 years ago.
Changes as suggested by @nacin (see associated comment for details)
ut.15155.diff (2.4 KB) - added by coffee2code 13 years ago.
WP unit tests for proposed patch.
15155.2-refresh.diff (1.4 KB) - added by DrewAPicture 12 years ago.
Refreshes 15155.2.diff
15155.3.diff (1.4 KB) - added by DrewAPicture 12 years ago.
shortcode_atts_{$shortcode}
ut.15155.2.diff (2.4 KB) - added by coffee2code 12 years ago.
Refresh of unit tests

Download all attachments as: .zip

Change History (45)

@coffee2code
14 years ago

Patch mentioned in ticket.

#1 @Denis-de-Bernardy
14 years ago

  • Cc Denis-de-Bernardy added

#2 @nkuttler
14 years ago

Yes, this looks like a good idea. Let's see if we can get this or something similar into 3.2 :-)

#3 @nkuttler
14 years ago

  • Cc wp@… added

#4 @markjaquith
14 years ago

  • Milestone changed from Awaiting Review to Future Release

This sounds really powerful.

#5 @WraithKenny
14 years ago

  • Cc Ken@… added

#6 @lancewillett
14 years ago

  • Cc lancewillett added

+1 for this. Have run into several cases lately where themes need to override the default gallery shortcode — and it's overkill to redo the entire gallery template just to change one parameter.

#7 @F J Kaiser
13 years ago

  • Cc 24-7@… added

+1 I was searching for a way to not rewrite the whole "walker" for the gallery shortcode and this solution is just ...wow(!): extremly powerful in lots of cases. @coffee2code - Thanks!

#8 @djr
13 years ago

Quote from coffeecode in ticket:14390

My approach presumes that if the user is excluding at least one image, they will know enough to be able to exclude the post thumbnail as well. And if for some reason they'd like to include the post thumbnail image as part of the gallery, they can still do so.

Food for thought: not all images are explicitly excluded. Some, like post_thumbnail, are automatically attached after publish_post has fired, e.g. by plugins like Regenerate Thumbnails (not sure) or Auto Post Thumbnail (the one we're using). See this forum post for a scenario where this patch wouldn't help. I'd really love to see an explicit 'use_post_thumbnail' [Yes/No] filter for the gallery shortcode.

Last edited 13 years ago by djr (previous) (diff)

#9 @djr
13 years ago

  • Cc jeanpaul@… added

#10 @djr
13 years ago

Added to this: the default order for most galleries is menu_order if I recall correctly? The problem I'm seeing is that *automatically* added post_thumbnails (with wp_insert_attachment() and wp_generate_attachment_metadata() ) aren't assigned an (ordinal) menu order ID in the Media Uploader Gallery tab, which is what the gallery shortcode expects for sorting by menu order. The added patch doesn't seem to consider sorting by (gallery) menu order, or at least not in my install.

Last edited 13 years ago by djr (previous) (diff)

#11 @chipbennett
13 years ago

  • Cc chip@… added

@coffee2code
13 years ago

Changes as suggested by @nacin (see associated comment for details)

#12 @coffee2code
13 years ago

As per consultation with @nacin, original patch has been modified and attached as 15155.2.diff:

  • Removed throwing warning (via either _deprecated_argument() or _doing_it_wrong()) if the optional $shortcode argument is not supplied
  • Removed generic shortcode_atts filter
  • Changed proposed filter name from shortcode_atts-$shortcode to shortcode_atts_$shortcode (dash to underscore)

Without throwing warnings to developers, though, I wonder how well (widely and quickly) the new argument will propagate... (though I acknowledge both _deprecated_argument() or _doing_it_wrong() are not appropriate to denote a newly added recommended argument). It would then become a matter of shortcode developers actively allowing their shortcode to be modified (by optionally specifying the argument) versus trying to make it so ANY shortcode's attributes could be modified (if we progressed to requiring, or strongly recommending, the argument).

#13 @coffee2code
13 years ago

Closed #18472 as a duplicate (it requested attribute handling that was specific to the gallery shortcode, which is more generically handled here).

#14 @kovshenin
13 years ago

  • Cc kovshenin@… added

#15 @doug
13 years ago

  • Cc wordpress@… added

#16 @adambackstrom
13 years ago

  • Cc adambackstrom added

#17 @carstenbach
13 years ago

  • Cc carstenbach added

@coffee2code
13 years ago

WP unit tests for proposed patch.

#18 @coffee2code
13 years ago

Patch 15155.2.diff still applies cleanly as of r20564.

Also attached WP unit test patch ut.15155.diff (patched against [UT694]) with tests that pass when the WP patch is applied.

#19 @nacin
12 years ago

  • Keywords 3.5-early added
  • Version changed from 3.0.1 to 2.5

Fine with this.

#20 @dougal
12 years ago

  • Cc dougal@… added

Please add this for 3.5. I was just wrestling with this very problem for a plugin. My use case is wanting to override the gallery shortcode defaults, because my plugin wants the 'link' att to default to 'file'.

#21 @navjotjsingh
12 years ago

  • Cc navjotjsingh@… added

#22 @lancewillett
12 years ago

Is this going to make it into 3.6?

#23 @markoheijnen
12 years ago

I think we should.

#24 @SergeyBiryukov
12 years ago

  • Milestone changed from Future Release to 3.6

#25 follow-up: @markjaquith
12 years ago

Yes please! Solid idea. Can someone verify that it still applies and tests cleanly? If it does, let's land it.

#26 @DrewAPicture
12 years ago

#23298 is (sorta) related.

@DrewAPicture
12 years ago

Refreshes 15155.2.diff

#27 @DrewAPicture
12 years ago

  • Description modified (diff)

#28 in reply to: ↑ 25 @DrewAPicture
12 years ago

Replying to markjaquith:

Yes please! Solid idea. Can someone verify that it still applies and tests cleanly? If it does, let's land it.

15155.2-refresh.diff applies cleanly and the copyright-appended-to-caption example works for me. Not sure about the tests.

@DrewAPicture
12 years ago

shortcode_atts_{$shortcode}

#29 @DrewAPicture
12 years ago

  • Cc xoodrew@… added

Kind of a nit for consistency, but 15155.3.diff changes the concat style in the filter from:

'shortcode_atts_' . $shortcode

to

"shortcode_atts_{$shortcode}"

@coffee2code
12 years ago

Refresh of unit tests

#30 @doug
12 years ago

  • Cc wordpress@… removed

#31 @coffee2code
12 years ago

Refreshed (ut.15155.2.diff) (along with minor doc/naming/spacing tweaks) the original unit tests so they apply cleanly against the since-revamped unit test code. Tests pass with any of the latest patches applied.

#32 @djr
12 years ago

  • Cc jeanpaul@… removed

#33 @betzster
12 years ago

  • Cc j@… added

#34 @goto10
12 years ago

  • Cc dromsey@… added

#35 @SergeyBiryukov
12 years ago

  • Keywords commit added

#36 @nacin
12 years ago

  • Owner set to nacin
  • Resolution set to fixed
  • Status changed from new to closed

In 23626:

Add shortcode_atts_$shortcode filter for when the name of the shortcode is passed to shortcode_atts(). props coffee2code. fixes #15155.

#37 @nacin
12 years ago

In 1239/tests:

Add shortcode_atts() filter tests. see #15155. props coffee2code.

#38 @DrewAPicture
12 years ago

  • Keywords 3.6-codex added

#39 @DrewAPicture
12 years ago

  • Keywords needs-codex added; 3.6-codex removed
Note: See TracTickets for help on using tickets.