How to Remove Shortcodes from Excerpt & Content in WordPress

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

Here are the code snippets that you can actually use to fix this issue and these snippets actually work for me.

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

In some WordPress websites, One thing I have noticed in their Excerpt text is, they display shortcodes as it is.

To remove these 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.

Share it to the Community!

You May Also Like These shortcode Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *