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.

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

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.

Leave a Reply

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

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top