shortcode_atts_(shortcode)filter-hookWP 3.6.0

Filters the default attributes of the specified shortcode.

The filter is called in shortcode_atts() only when its third $shortcode parameter (the shortcode name) is specified. That is, when the function is called for an actual shortcode; for example, see gallery_shortcode().

Basic hook variants:

  • shortcode_atts_gallery
  • shortcode_atts_caption
  • shortcode_atts_playlist
  • shortcode_atts_audio
  • shortcode_atts_video

Usage

add_filter( 'shortcode_atts_(shortcode)', 'wp_kama_shortcode_atts_filter', 10, 4 );

/**
 * Function for `shortcode_atts_(shortcode)` filter-hook.
 * 
 * @param array  $out       The output array of shortcode attributes.
 * @param array  $pairs     The supported attributes and their defaults.
 * @param array  $atts      The user defined shortcode attributes.
 * @param string $shortcode The shortcode name.
 *
 * @return array
 */
function wp_kama_shortcode_atts_filter( $out, $pairs, $atts, $shortcode ){

	// filter...
	return $out;
}
$out(array)
An associative array of shortcode attributes that will be changed during filtering.
$pairs(array)
An array of all possible shortcode attributes and their default values.
$atts(array)
The attributes specified by the user for the shortcode.
$shortcode(string)
The shortcode name, for example, gallery. This name is appended to the filter name.

Examples

#1 Change the default number of gallery columns

By default, a gallery has 3 columns, but suppose 2 are required. If the shortcode in the text is [gallery_ ids="54,65,65"], the images are output in 3 columns, but they need to be output in 2.

Use the shortcode_atts_gallery hook and add the resulting code to the theme's functions.php file:

## Set two columns as the gallery default
add_filter( 'shortcode_atts_'.'gallery', 'set_default_gallery_columns', 10, 3);
function set_default_gallery_columns( $out, $pairs, $atts ){
	// Change the default if the value is not set
	if( ! isset($atts['columns']) ){
		$out['columns'] = 2;
	}

	return $out;
}

Changelog

Since 3.6.0 Introduced.
Since 4.4.0 Added the $shortcode parameter.

Where the hook is called

shortcode_atts()
shortcode_atts_(shortcode)
wp-includes/shortcodes.php 690
$out = apply_filters( "shortcode_atts_{$shortcode}", $out, $pairs, $atts, $shortcode );

Where the hook is used in WordPress

wp-includes/widgets/class-wp-widget-custom-html.php 131
add_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ) );
wp-includes/widgets/class-wp-widget-custom-html.php 165
remove_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ) );
wp-includes/widgets/class-wp-widget-text.php 270
add_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ) );
wp-includes/widgets/class-wp-widget-text.php 323
remove_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ) );