Using WordPress ‘pre_do_shortcode_tag’ PHP filter

The pre_do_shortcode_tag filter allows you to modify or replace a shortcode’s output before it is executed, effectively short-circuiting the shortcode generation process.

Usage

add_filter('pre_do_shortcode_tag', 'your_custom_function', 10, 4);

function your_custom_function($output, $tag, $attr, $m) {
    // your custom code here
    return $output;
}

Parameters

  • $output (false|string): The short-circuit return value, either false or the value to replace the shortcode with.
  • $tag (string): The shortcode name.
  • $attr (array|string): The shortcode attributes as an array or an empty string.
  • $m (array): The regular expression match array.

More information

See WordPress Developer Resources: pre_do_shortcode_tag

Examples

To modify the output of the [gallery] shortcode, you can use the following code:

add_filter('pre_do_shortcode_tag', 'modify_gallery_shortcode', 10, 4);

function modify_gallery_shortcode($output, $tag, $attr, $m) {
    if ('gallery' !== $tag) {
        return $output;
    }

    // Modify gallery output as needed
    $new_output = "Modified gallery output";

    return $new_output;
}

Replace [video] shortcode with a custom video player

If you want to replace the [video] shortcode with a custom video player, use this code:

add_filter('pre_do_shortcode_tag', 'replace_video_shortcode', 10, 4);

function replace_video_shortcode($output, $tag, $attr, $m) {
    if ('video' !== $tag) {
        return $output;
    }

    // Generate custom video player output
    $custom_video_output = "Custom video player output";

    return $custom_video_output;
}

Prevent [audio] shortcode from rendering

To prevent the [audio] shortcode from rendering its output, use the following code:

add_filter('pre_do_shortcode_tag', 'prevent_audio_shortcode', 10, 4);

function prevent_audio_shortcode($output, $tag, $attr, $m) {
    if ('audio' === $tag) {
        return ''; // Return an empty string to prevent the shortcode output
    }

    return $output;
}

Add a custom attribute to [caption] shortcode

Add a custom attribute to the [caption] shortcode with this code:

add_filter('pre_do_shortcode_tag', 'add_custom_attr_to_caption', 10, 4);

function add_custom_attr_to_caption($output, $tag, $attr, $m) {
    if ('caption' !== $tag) {
        return $output;
    }

    // Add custom attribute to the caption shortcode
    $attr['custom_attr'] = 'custom_value';

    // Rebuild the caption output with the new attribute
    $new_output = "Caption output with custom attribute";

    return $new_output;
}

Display a message for users without access to [private] shortcode content

Display a custom message for users who don’t have access to the [private] shortcode content:

add_filter('pre_do_shortcode_tag', 'private_shortcode_for_logged_in_users', 10, 4);

function private_shortcode_for_logged_in_users($output, $tag, $attr, $m) {
    if ('private' !== $tag) {
         return $output;
    }
    if (is_user_logged_in()) {
         return $output; // Allow logged-in users to see the shortcode output
     }

     // Custom message for users without access
     $custom_message = "You must be logged in to view this content.";

     return $custom_message;
}