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

Filters the output created by a shortcode callback.


Description Description


Parameters Parameters

$output

(string) Shortcode output.

$tag

(string) Shortcode name.

$attr

(array|string) Shortcode attributes array or empty string.

$m

(array) Regular expression match array.


Top ↑

Source Source

File: wp-includes/shortcodes.php

View on Trac


Top ↑

Changelog Changelog

Changelog
Version Description
4.7.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Aurovrata Venet

    This filter is very useful to either add additional content at the end of a shortcode (for example an inline script),

    add_filter( 'do_shortcode_tag','add_my_script',10,3);
    function enqueue_my_script($output, $tag, $attr){
      if('aShortcode' != $tag){ //make sure it is the right shortcode
        return $output;
      }
      if(!isset($attr['id'])){ //you can even check for specific attributes
        return $output;
      }
      $output.='<script> ... </script>';
      return $output;
    }
  2. Skip to note 2 content
    Contributed by Aurovrata Venet

    Another useful functionality is to enqueue shortcode-specific scripts,

    add_action( 'wp_enqueue_scripts', 'register_my_script');
    function register_my_script(){
      wp_register_script('my-shortcode-js',$src, $dependency, $version, $inFooter);
    }
    add_filter( 'do_shortcode_tag','enqueue_my_script',10,3);
    function enqueue_my_script($output, $tag, $attr){
      if('myShortcode' != $tag){ //make sure it is the right shortcode
        return $output;
      }
      if(!isset($attr['id'])){ //you can even check for specific attributes
        return $output;
      }
      wp_enqueue_script('my-shortcode-js'); //enqueue your script for printing
      return $output;
    }

    first you register your script, which doesn’t actually get printed on your page unless your enqueue it if your shortcode is called.

You must log in before being able to contribute a note or feedback.