WordPress Shortcodes API Hooks

Filters and actions available in the WordPress Shortcodes API.


Filters

pre_do_shortcode_tag

Short-circuits shortcode processing before the callback is called.

apply_filters( 'pre_do_shortcode_tag', false|string $output, string $tag, array $attr, array $m )

Parameters:

  • $output (false|string) — Return value. false to continue, or string to short-circuit.
  • $tag (string) — Shortcode name
  • $attr (array) — Parsed attributes (always array since 6.5.0)
  • $m (array) — Full regex match array

Returns: false to process normally, or string to replace the shortcode

Since: 4.7.0 (6.5.0: $attr is always array)

Example:

// Cache shortcode output
add_filter( 'pre_do_shortcode_tag', function( $output, $tag, $attr, $m ) {
    if ( 'expensive_shortcode' !== $tag ) {
        return false; // Not our shortcode, continue normally
    }
    
    $cache_key = 'shortcode_' . md5( serialize( $attr ) );
    $cached = wp_cache_get( $cache_key );
    
    if ( false !== $cached ) {
        return $cached; // Return cached output
    }
    
    return false; // Not cached, process normally
}, 10, 4 );

do_shortcode_tag

Filters the output after a shortcode callback executes.

apply_filters( 'do_shortcode_tag', string $output, string $tag, array $attr, array $m )

Parameters:

  • $output (string) — Shortcode output from callback
  • $tag (string) — Shortcode name
  • $attr (array) — Parsed attributes (always array since 6.5.0)
  • $m (array) — Full regex match array

Returns: Modified output string

Since: 4.7.0 (6.5.0: $attr is always array)

Example:

markup
// Wrap all shortcode output in a container
add_filter( 'do_shortcode_tag', function( $output, $tag ) {
    return '<div class="shortcode-wrapper shortcode-' . esc_attr( $tag ) . '">' 
           . $output 
           . '</div>';
}, 10, 2 );
// Cache shortcode output after processing
add_filter( 'do_shortcode_tag', function( $output, $tag, $attr ) {
    if ( 'expensive_shortcode' !== $tag ) {
        return $output;
    }
    
    $cache_key = 'shortcode_' . md5( serialize( $attr ) );
    wp_cache_set( $cache_key, $output, '', HOUR_IN_SECONDS );
    
    return $output;
}, 10, 3 );

shortcode_atts_{$shortcode}

Filters the merged shortcode attributes for a specific shortcode.

apply_filters( "shortcode_atts_{$shortcode}", array $out, array $pairs, array $atts, string $shortcode )

Parameters:

  • $out (array) — Merged attributes (defaults + user values)
  • $pairs (array) — Original defaults
  • $atts (array) — User-provided attributes
  • $shortcode (string) — Shortcode name

Returns: Modified attributes array

Since: 3.6.0 (4.4.0: added $shortcode parameter)