Wordpress Custom Search

WordPress Override the Search Results Template Page

Looking for ways to override the Search Results Page with a custom template file?

1. Create a plugin directory

For example, create a plugin named: custom-search in wp-content/plugins and call the file custom-search.php.

2. Create a search page

First, go to your themes search page, for example:

/wp-content/themes/graphy/search.php

Does your theme has no search page? Here you find a sample code for a search page.

Have your search page in place, call it something like search-template.php and put it in the same directory as the plugin.

3. Create plugin that override search functionality

<?php
/*
Plugin Name: Custom Search Template
Plugin URI: Plugin URL
description: Replace custom search template
Version: 1.0
Author: YourCompany or name
Author URI: https://www.yoursite.com
*/

add_filter('template_include', function($template) {
    global $wp_query;

    if ($wp_query->is_search && !empty($_GET['s']))
    {
        return dirname(__FILE__) . "/search-template.php";
    }

    return $template;

}, 1);

4. Activate the plugin

Activate the plugin and search. Hopefully, your new custom search page shows up.

Happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

en_USEnglish
Scroll to Top