Remove Shortcode from Excerpt and Content in WordPress

Is your WordPress website showing shortcodes in the excerpt or do you want to remove shortcodes from content?.

Remember: These methods will work only if the shortcodes are registered with WordPress. So if you are trying to remove shortcodes those got remained in your site after deactivation and deleting the plugin then this will not work, cause now those shortcodes are not shortcodes anymore, they are just a piece of text for WordPress.

Remove Shortcodes From Excerpt

To remove shortcodes from excerpt text, all you need to do is just copy the below code and add it into your theme’s functions.php file.


// Remove Shortcodes from excerpt
function diwp_remove_shortcode_from_excerpt($content) {
  
    $content = strip_shortcodes( $content );
 	
 	return $content;
}

add_filter('the_excerpt', 'diwp_remove_shortcode_from_excerpt');

That’s it. You’re done.

Remove Shortcodes From Content or Posts

If you want to remove shortcodes from content then you can use the below snippet.

function diwp_remove_shortcode_from_content($content) {
  
    $content = strip_shortcodes( $content );
 	
 	return $content;
}

add_filter('the_content', 'diwp_remove_shortcode_from_content');

Remove Shortcodes From Homepage | Frontpage.

If you have a different page as Homepage, instead of default blog listing page then you can use the below code.

function diwp_remove_shortcode_from_homepage($content) {
 	// this will check if the page is front page
if ( is_front_page() ) {
 	   $content = strip_shortcodes( $content );
 	}	
 	return $content;
}

add_filter('the_content', 'diwp_remove_shortcode_from_homepage');

Remove Shortcodes From Blog Posts Listing But Not From Single Post.

If you just want to remove shortcodes from blog posts listing but not from single post then you can use the below code.

function diwp_remove_shortcode_from_blog_posts_content($content) {
 	// this will check if the page is homepage
if ( is_home() ) {
 	   $content = strip_shortcodes( $content );
 	}	
 	return $content;
}

add_filter('the_content', 'diwp_remove_shortcode_from_blog_posts_content');

I hope this article helped you to learn about remove shortcodes from excerpt and content in WordPress.

Owner of diveinwp.com, solely responsible for creating helpful, informative content about WordPress, a fulltime WordPress developer with 8+ years of hands-on experience in WordPress design and development.