remove_shortcode()
Deletes a registered shortcode.
Shortcodes should be removed as late as possible, ideally before any content is output. WP shortcodes are registered very early, even before the plugins_loaded action. Plugin shortcodes are usually also registered early before the init action.
To remove shortcodes from text, use strip_shortcodes().
No Hooks.
Returns
null. Returns nothing.
Usage
remove_shortcode( $tag );
- $tag(string) (required)
- The name of the shortcode whose handler needs to be removed.
Examples
#1 Remove the gallery shortcode
By default WordPress uses the [gallery] shortcode, which outputs the gallery. Let's remove the handler of this shortcode by inserting the following code before displaying the content:
remove_shortcode( 'gallery' ); // ... the_content();
#2 Disable the [[gallery]] shortcode completely
If, for example, you insert in functions.php:
remove_shortcode( 'gallery' );
then when viewing the post, the shortcode in the content will not be processed and will be displayed as is:
[gallery ids="375,291,166"]
Note: in the admin, in the visual editor, the gallery will be supported...
#3 Disable the shortcode and cut it's code from the content
There is a shortcode myshortcode we need to disable it and cut all its variants from the post content.
To cut, use the the_content filter hook.
add_action( 'init', 'remove_and_strip_shortcode' );
function remove_and_strip_shortcode() {
// delete
remove_shortcode( 'myshortcode' );
// cut out
add_filter( 'the_content', '_strip_myshortcode', 5 );
}
function _strip_myshortcode( $content ){
// cut: [myshortcode] and [myshortcode ids="132,2154,548"]
$content = preg_replace( '~\[myshortcode[^\]]*\]~', '', $content );
// cut: [myshortcode] data [/myshortcode]
$content = preg_replace( '~\[(myshortcode)[^\]]*\].*?\[\/\]~s', '', $content );
return $content;
} #4 Demo: add and remove custom shortcode
add_action( 'init', 'add_my_shortcodes' );
add_action( 'init', 'remove_my_shortcodes', 20 );
// add a custom shortcode
function add_my_shortcodes() {
add_shortcode( 'myShortcode', 'my_shortcode_function' );
}
// remove it
function remove_my_shortcodes() {
remove_shortcode( 'myShortcode' );
}
Notes
- Global. Array. $shortcode_tags
Changelog
| Since 2.5.0 | Introduced. |
remove_shortcode() remove shortcode code WP 6.8.3
function remove_shortcode( $tag ) {
global $shortcode_tags;
unset( $shortcode_tags[ $tag ] );
}