WordPress: Remove shortcodes from excerpt on the front page

Shortcodes offer a great flexibility for including plugins in your content on a certain position, but during the repositioning of the ad blocks using the shortcodes, provided by the adrotate plugin, I got confronted with the following problem:

Is it possible to use shortcodes in the content before the occurence of the more – tag such that it gets handled as intended in the “full view” of the posting but is hidden in the index of the front page?

In my case it was important to hide the ad blocks on the front page, otherwise each excerpt in the index would contain an ad which is not acceptable.

Solution

Add one of the following code snippets to the functions.php of your theme

  • Remove all shortcodes from the content
    		function remove_shortcode_from_index($content) {
    			if ( is_home() ) {
    				$content = strip_shortcodes($content);
    			}
    			return $content;
    		}
    		add_filter('the_content', 'remove_shortcode_from_index');
    		
  • Remove an specific shortcode (e.g. adrotate)
    		function dummy_adrotate_shortcode($atts) {
    			return "";
    		}
    
    		//remove shortcode from content
    		function remove_shortcode_from_index($content) {
    
    			//check for front page
    			if (is_home()) {
    				//remove specific shortcode
    				remove_shortcode('adrotate');
    				//add dummy shortcode which returns a blank string
    				add_shortcode('adrotate', 'dummy_adrotate_shortcode');
    			}
    			return $content;
    		}
    
    		//hook into filtermanagement
    		add_filter('the_content', 'remove_shortcode_from_index');
    		
This entry was posted in Coding, Microtutorials, PHP, Web, Wordpress and tagged , , , , . Bookmark the permalink.

3 Responses to WordPress: Remove shortcodes from excerpt on the front page

  1. atif says:

    nice ……………. its help me in my project

  2. Pingback: Top 160 Tutoriais WordPress – incluindo Basico, Desenvolvimento e Plugins – Site para Empresas – Blog sobre Internet e Criação de Site

  3. Pingback: Mejoras gráficas para el blog | La Bitácora del Tigre

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>