The do_shortcode_tag WordPress PHP filter allows you to modify the output created by a shortcode callback.
Usage
add_filter('do_shortcode_tag', 'my_custom_filter', 10, 4);
function my_custom_filter($output, $tag, $attr, $m) {
// your custom code here
return $output;
}
Parameters
$output(string) – Shortcode output.$tag(string) – Shortcode name.$attr(array|string) – Shortcode attributes array or empty string.$m(array) – Regular expression match array.
More information
See WordPress Developer Resources: do_shortcode_tag
Examples
Change the output of a specific shortcode
Modify the output of the [example_shortcode]:
add_filter('do_shortcode_tag', 'change_example_shortcode_output', 10, 4);
function change_example_shortcode_output($output, $tag, $attr, $m) {
if ('example_shortcode' === $tag) {
$output = '<strong>' . $output . '</strong>';
}
return $output;
}
Modify all shortcode outputs
Append a string to all shortcode outputs:
add_filter('do_shortcode_tag', 'append_to_all_shortcodes', 10, 4);
function append_to_all_shortcodes($output, $tag, $attr, $m) {
$output .= ' - Custom Text';
return $output;
}
Add CSS classes to shortcode output
Add a CSS class to the output of a specific shortcode [custom_shortcode]:
add_filter('do_shortcode_tag', 'add_css_class_to_custom_shortcode', 10, 4);
function add_css_class_to_custom_shortcode($output, $tag, $attr, $m) {
if ('custom_shortcode' === $tag) {
$output = '<span class="my-custom-class">' . $output . '</span>';
}
return $output;
}
Modify shortcode output based on attributes
Change the output of [conditional_shortcode] based on its type attribute:
add_filter('do_shortcode_tag', 'change_output_based_on_attr', 10, 4);
function change_output_based_on_attr($output, $tag, $attr, $m) {
if ('conditional_shortcode' === $tag && isset($attr['type'])) {
if ('bold' === $attr['type']) {
$output = '<strong>' . $output . '</strong>';
} elseif ('italic' === $attr['type']) {
$output = '<em>' . $output . '</em>';
}
}
return $output;
}
Remove specific shortcode output
Disable the output of [disabled_shortcode]:
add_filter('do_shortcode_tag', 'disable_specific_shortcode', 10, 4);
function disable_specific_shortcode($output, $tag, $attr, $m) {
if ('disabled_shortcode' === $tag) {
$output = '';
}
return $output;
}