How to Add a Custom Search Box in WordPress?
Adding a custom search box in WordPress is a relatively simple process. First, you will need to create a search form plugin.
Once you have created the plugin, you will need to add the following code to your plugin’s functions.php file:.
// Add search box function add_search_box() { add_action( ‘admin_menu’, ‘add_search_box_admin’); }
Next, you will need to create a file called search-box.php and populate it with the following code:
add_button(‘Search’, __(‘Search’), array( ‘type’ => ‘text’, ‘default’ => __(‘Search’), ‘class’ => ‘search-box-button’ )); $search_box->add_input(‘query’, ‘text’, ‘utf-8′, ‘required’, ‘size’ => 30, ‘class’ => ‘search-box-input’); $search_box->add_input(‘submit’, ‘submit’); $search_box->add_output(__(‘Results’), ‘textarea’); } /** * Add admin search box */ function add_search_box_admin() { add_action( ‘admin_menu’, ‘add_search_box_admin’); }
To use the search box, simply add it to any post or page’s admin panel. To add it to a post’s admin panel, simply add the following code to the post’s header:
Finally, add the following code to your plugin’s functions.php file to enable the search box:
// Enable search box add_filter(‘the_content’, ‘add_search_box’);
That’s all there is to it! Your custom search box will now be available in WordPress.