ThemeShaper Forums » Thematic

[closed]

Shortcode content not showing up in search results

(7 posts)
  • Started 8 years ago by kfrank
  • Latest reply from ScottNix
  • This topic is not resolved
  1. kfrank
    Member

    I am using shortcodes on pages to make it easy for my client to edit the content is there a reason that shortcode content(the text between the shortcode tags) does not show up in the search results, excerpted like the posts do? For instance I am using something like:

    [member-info team="mets" number="33" position="pitcher"]
    Lorem ipsum dolor sit amet, abbas, et dolore minim quibus opto iustum augue, blandit      nunc scisco, letalis consequat consectetuer iusto. Wisi, brevitas tum sed esca esse facilisi in, turpis virtus.
    [/member-info]

    Is there any way that I can get my shortcode content(excerpted like my posts) to show up in my search results?

    I have created a child theme from the Thematic framework.

    Thanks for any help on this

    Posted 8 years ago #
  2. middlesister
    Member

    I'm not sure I follow you in what you want to happen vs what is happening.
    Do you have the shortcodes on wordpress Pages, and if so are there more than one shortcode per page?
    Is the other content from the pages showing up in the search results, and just the shortcode content somehow missing?
    Or is it so that you want each shortcode content to be a separate search result?

    There is a plugin called search everything (http://wordpress.org/extend/plugins/search-everything/) but I wonder if that is the issue here. To wordpress, the content in between shortcodes are not a distinguished piece of information that can get excerpted like posts. You would be better suited to use a custom post type for that. The text in between the codes should be searchable though, as part of the page/post they are in.

    Posted 8 years ago #
  3. I happened to be playing with some content styling shortcodes. You are correct in that page content held within a shortcode won't show on search.

    An option to get around it is to actually add an excerpt to the page, http://codex.wordpress.org/Function_Reference/add_post_type_support.

    I wasn't able to figure out a solution to get them to show, someone has probably gone through this process before, but I couldn't find it. I hate posting to examples which won't be around forever, but here is an example of the problem. http://scottnix.com/test2/?s=one_half&searchsubmit=Search one page has a excerpt, one page does not, and one post does not. Looks like it effects pages and posts without excerpts.

    Posted 8 years ago #
  4. middlesister
    Member

    This is interesting.
    The reason might be because shortcodes are processed with filters on the_content and that the_excerpt does not apply that filter.
    You could try adding that filter to excerpts as well. The priority seems to be important, try using the same number as for the_content and change it if it doesn't work.

    add_filter('the_excerpt', 'do_shortcode', 11);

    Posted 8 years ago #
  5. Tested the above code you provided middlesister. This allows you to use the shortcodes in the excerpt, which is good to know, but still requires adding manual excerpts (with or without shortcodes) to posts and pages to get them to show text on search.

    Just adding the code won't "show" the text hidden between shortcodes for the search function. The search is "finding" the text in the shortcodes, just not outputting the text to the search preview (it grabs what is not held in shortcodes), hope that makes a little sense.

    I will keep looking, surely this is a problem for a lot of people.

    Posted 8 years ago #
  6. middlesister
    Member

    Aha I see. So, the_excerpt filter is only working on the manually written excerpts, while search results are showing the automatically generated ones?
    Then I think I know where the culprit is: wp_trim_excerpt() - http://codex.wordpress.org/Function_Reference/wp_trim_excerpt.
    It uses strip_shortcodes() on the text before trimming it to 55 characters and adding the […] and so on.

    Excerpts can be annoying at times to get to where you want them. Helgatheviking made a nice thread at
    http://forums.themeshaper.com/topic/how-to-make-excerpts-roll-over-and-say-uncle

    You could maybe use her technique of replacing wp_trim_excerpt with your own version, commenting out the ´strip_shortcodes()` part:

    //Replace wp_trim_excerpt with a commented out strip_shortcodes()
    function improved_trim_excerpt($text) {
    	$raw_excerpt = $text;
    	if ( '' == $text ) {
    		$text = get_the_content('');
    
    		//$text = strip_shortcodes( $text );
    
    		$text = apply_filters('the_content', $text);
    		$text = str_replace(']]>', ']]>', $text);
    		$text = strip_tags($text);
    		$excerpt_length = apply_filters('excerpt_length', 55);
    		$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
    		$words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
    		if ( count($words) > $excerpt_length ) {
    			array_pop($words);
    			$text = implode(' ', $words);
    			$text = $text . $excerpt_more;
    		} else {
    			$text = implode(' ', $words);
    		}
    	}
    	return apply_filters('improved_trim_excerpt', $text, $raw_excerpt);
    }
    
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'improved_trim_excerpt');

    You might also need to add
    $text = do_shortcode($text);
    in order to make sure the shortcodes are actually parsed and not just displayed.

    Posted 8 years ago #
  7. The solution above to comment out the strip_shortcodes above works great.

    On a side note, if you add excerpts functionality to "pages" through functions php (http://codex.wordpress.org/Function_Reference/add_post_type_support), and then remove it. The excerpts which you manually added to pages will still show, they have to be manually removed also.

    Thanks kfrank for notifying me of a problem I was eventually going to run into, and thanks middlesister for the solution.

    Posted 8 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.