skip to Main Content

I have a WordPress website where I use the following shortcode to insert content:

[page name="foo/bar/buz"]

The shortcode is handled in the functions.php file as follows:


function page_shortcode($attr)
{
    $name = $attr['name'];
    $locale = get_locale() === 'fr_FR' ? 'fr' : 'en';
    $path = "/content/{$locale}/pages/{$name}.md";
    $page = file_get_contents(WP_CONTENT_DIR . $path);

    return do_shortcode($page);
}

I’m utilizing these shortcodes to delegate to specific files, making it convenient to manage them in a separate repository and handle multiple languages. However, the native WordPress search is not functioning correctly, presumably because the content isn’t stored in the database.

Is there a workaround or solution to make the WordPress search function correctly with shortcode-driven content? Any insights or suggestions would be greatly appreciated.

2

Answers


  1. The search function in core WordPress only examines post content, titles, and other stuff in the dbms. The Relevanssi search plugin has an option to expand shortcodes when creating its search index, however.

    You could create your own plugin to do this kind of search if you didn’t want to use a prepackaged one. But that would be a lot of work. Explaining it would be far beyond the scope of a Stack Overflow answer. And making such a search process fast enough to use at scale would result in, well, a search plugin like Relevanssi.

    Login or Signup to reply.
  2. Take a look at the "Include Custom / API Results" answer in this question. It looks like it could be a partial solution for you to get your pages in the search results, but like mentioned by O.Jones, you will need to write your own search routine to look through the files themselves.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search