Support » Plugin: Relevanssi - A Better Search » Shortcode name and param are being indexed

  • When the option “Expand shortcodes when indexing” is off, Relevanssi indexes the name and the parameters of certain shortcodes. For example, a page containing
    has the following index terms: contact form email

    I traced this down to the fact that certain plugins (at least two: Contact Form to Email and Newsletter) do not register their shortcodes when in admin, i.e. their initialization code does something like

    if ( is_admin() ) {    
    } else {    

    As a result, strip_shortcodes does not remove the shortcodes because they are not in global $shortcode_tags when Relevanssi indexing is run.

    I think I could probably work around this using the ‘relevanssi_post_content’ filter, but if you are able to provide a more general solution, others would benefit as well.

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


    That’s annoying when plugins do that…

    The most general solution would be to automatically remove everything that looks like a shortcode, but I’m not sure I want to do that. There may be a good reason for having something like that on a post.

    I guess the best way would be to just remove those two shortcodes. What is the shortcode for Newsletter?

    Thanks a lot for your attention to this issue!

    I agree that removing everything in square brackets is probably not a good idea.
    I am not sure what you mean by “remove the shortcodes”. Remember that the source of the problem is that the shortcodes have not been added in this scenario.

    Anyway, to answer your question, the Newsletter plugin has 3 shortcodes that are *not* defined when is_admin():
    [newsletter] [newsletter_form] [newsletter_field]

    The latter, [newsletter_field], may occur once or more inside [newsletter_form]…[/newsletter_form]

    The other plugin only has one shortcode [CONTACT_FORM_TO_EMAIL]

    Plugin Author Mikko Saari


    I meant the shortcodes as plain text, ie. something like this in a relevanssi_post_content filter function:

    $string = preg_replace( '/\[contact_form_to_email.*?\]/i', '', $string );
    $string = preg_replace( '/\[newsletter.*?\]/i', '', $string );
    $string = preg_replace( '/\[newsletter_form.*?\].*?\[\/newsletter_form\]/i', '', $string );

    That would probably work, and I’d be happy to solve my problem in a filter.
    When shortcode expansion is turned off, the following would also achieve the goal:

    if (!shortcode_exists('CONTACT_FORM_TO_EMAIL')) {
       add_shortcode('CONTACT_FORM_TO_EMAIL', function() { return ''; });

    etc. so that the “undefined in admin” shortcodes are stripped by strip_shortcodes, like the others.

    You could even make Relevanssi do that automatically if there were an option similar to (or the same as) relevanssi_disable_shortcodes that allows users to enter the list of shortcodes to be ignored during indexing when shortcode expansion is turned off.

    Which brings me to another point: when shortcode expansion is turned on, the names and parameters of any shortcodes disabled using relevanssi_disable_shortcodes (or as part of your automatic list) are still indexed.
    For example, if I have the following in my post:
    [sc parameter=”value”]
    and I add ‘sc’ to the list of disabled shortcodes, the words “parameter” and “value” are included in the index.

    I hope you don’t mind me being so picky. I may have gone a bit overboard in my evaluation of Relevanssi before I pay for the premium version 🙂

    Plugin Author Mikko Saari


    That’s a good idea. I adjusted the current code a bit:

    foreach ( $shortcodes as $shortcode ) {
    	remove_shortcode( trim( $shortcode ) );
    	add_shortcode( $shortcode, '__return_empty_string' );

    $shortcodes now includes the shortcodes from the relevanssi_disable_shortcodes option and the default list. Instead of just removing, I’ll re-add the shortcode with the empty string function.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Shortcode name and param are being indexed’ is closed to new replies.