remove_shortcode()WP 2.5.0

Removes hook for shortcode.

No Hooks.

Return

null. Nothing.

Usage

remove_shortcode( $tag );
$tag(string) (required)
Shortcode tag to remove hook for.

Examples

0

#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();
0

#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...

0

#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;        
}
0

#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() code WP 6.1.1

function remove_shortcode( $tag ) {
	global $shortcode_tags;

	unset( $shortcode_tags[ $tag ] );
}