Support » Plugin: Relevanssi - A Better Search » Shortcodes in excerpts are not expanded

  • Resolved twhb

    (@twhb)


    I have both the “Index the post excerpt” and “Expand shortcodes when indexing” settings on, and have rebuilt the index after turning them on. However, search result excerpts are still displaying shortcode source code, and searches for content rendered by shortcodes return no results while searches for words in shortcode source code return the posts with that code.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Is it possible the shortcodes are not always registered? In that case they might not be active when Relevanssi tries to expand them, and that would lead to what you’re seeing.

    Also, Relevanssi automatically expands shortcodes only in post content, so if the shortcodes appear somewhere else (like in custom fields), they need to be expanded manually.

    Thread Starter twhb

    (@twhb)

    It’s the second issue. Sorry, I tried to say that but I guess “excerpt” here is ambiguous. It’s the field WooCommerce uses for short descriptions, which I believe is called the “excerpt” in WordPress core.

    How would one go about expanding them manually? I tried adding add_filter('get_the_excerpt', 'do_shortcode'); to functions.php, but while this does render shortcodes on the search results page, the search results are still chosen based on the un-rendered source code. The rendering also isn’t correct, it appears to happen before whatever filter does things like turn a bulleted list into inline text.

    Plugin Author Mikko Saari

    (@msaari)

    add_filter( 'relevanssi_post_to_index', 'rlv_excerpt_shortcodes' );
    function rlv_excerpt_shortcodes( $_post ) {
      $_post->post_excerpt = do_shortcode( $_post->post_excerpt );
      return $_post;
    }

    This should do the trick for indexing. I’m not sure it’s possible at the moment to make it work in the excerpts, I need to add some extra filters to Relevanssi to make it possible.

    Thread Starter twhb

    (@twhb)

    Got it working. Thank you! As you said, the snippets in search results still aren’t there, but with the content indexed and visitors no longer seeing source code, this is much less of a problem. I’ll just keep an eye on the changelog.

    (A note to any future readers, after adding the above code you need to rebuild the Relevanssi index.)

    Plugin Author Mikko Saari

    (@msaari)

    If you want to give the new filter a go, it’s here: https://github.com/msaari/relevanssi/commit/166743bd897000110d5e36f02a4ef126b3ea64c5

    It works exactly like relevanssi_post_to_index, so you can just hook the same function to that filter.

    • This reply was modified 2 years, 8 months ago by Mikko Saari.
    Thread Starter twhb

    (@twhb)

    And now that’s working too. Thank you so much, I definitely didn’t expect such a fast patch.

    Plugin Author Mikko Saari

    (@msaari)

    If you’re happy with the service provided, please consider leaving a review 😉

    Thread Starter twhb

    (@twhb)

    Already did 🙂

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Shortcodes in excerpts are not expanded’ is closed to new replies.