Short Codes in site search results

I am having a problem with Visual Composer shortcodes being returned in site search results.
Here is an example:
http://cityoffresno.staging.wpengine.com/site-search/?phrase=key+staff

As you can see there are multiple shortcodes in the results like this: [vc_column width="2/3"]. How can we exclude those kinds of things from showing up in the results? It would look fine without anything between brackets [anything]
We are trying to find a suitable solution to replace Google's Custom Site Search since it will be shut down on 4/1.

Thank you,
Gavin

  • James Morris
    • WordPress Enthusiast

    Hello Gavin Haubelt,

    I hope you are well today.

    By the looks of things, you are using a third-party plugin for performing the content search. Unfortunately, it looks like that plugin would require some rewrites to accommodate what you're trying to do. The problem appears to be that the plugin is searching the_content() without any consideration for shortcodes, which would take custom coding that plugin to overcome.

    A better approach would be to roll your own custom search using the WordPress native functionality. We've written a great blog post about this very thing at https://premium.wpmudev.org/blog/build-your-own-custom-wordpress-search/ The benefit of this is WordPress natively knows to exclude shortcodes from the search results.

    To move forward with this even more, you can even template the Search Results page to get the exact look you want. See: https://codex.wordpress.org/Creating_a_Search_Page

    While this does take a little bit of time and reading to accomplish, it's a much better approach because you are leveraging the native WordPress functionality instead of relying a plugin that does not understand the difference between natural text and shortcodes. :wink:

    I hope this clarifies a bit. Let us know if you have any further questions. We'll be happy to help! :slight_smile:

    Best regards,

    James Morris

  • Gavin Haubelt
    • New Recruit

    I am using WPMUDEV search plugin to do search! I have a multi-site and am using the Site Search Plugin so that isn't an acceptable answer.
    I am paying $50 a month to use this site search plugin and I would it expect to work correctly. I am also using the WPMUDEV index plugin to index the site.
    Thanks,
    Gavin

  • Ash
    • WordPress Hacker

    Hello Gavin Haubelt

    Let's try editing the plugin file and to check if it works :slight_smile:

    Please go to /global-site-search/templates/global-site-search.php line no 70 and replace

    <?php $the_content = preg_replace( "~(?:\[/?)[^/\]]+/?\]~s", '', network_get_the_content() ); ?>

    with

    <?php $the_content = strip_shortcodes( preg_replace( "~(?:\[/?)[^/\]]+/?\]~s", '', network_get_the_content() ) ); ?>

    Please let me know if it works for you. Have a nice day!

    Cheers,
    Ash

  • Ash
    • WordPress Hacker

    Well, this is really strange as this works for me on my test site. For some reason, it's not working on your site, so I used wordpress' native function to strip shortcode and it worked :slight_smile: It might be due to any plugin conflict.

    Also note that, you need to make a note about these changes as when the update will come, the changes will be wiped out and then you have to manually make the changes again.

    Have a nice day!

    Cheers,
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.