Adding Ajax Search Box at Astra Header Search form

Ajax Search Box

You can easily add the toggle search form at Astra theme header. Theme have inbuilt option in customizer about it. Search icon is appearing at right side of the header menu. I shall add the Ajax logic in this inbuilt search form and it will fetch the search result based on search key without the page refresh. I am sharing the full steps at below:

Minimum Requirements:

  1. Astra Theme
  2. Astra Child Theme (you can use your built-in astra child theme)

Enabling The Search Icon At Header

  • Login to Dashboard
  • Navigate to Appearance -> Customize link
  • Click on Layout -> Header -> Primary Header
  • Select “Search” option from Last Item In Menu drop down list
  • Click on Publish button
  • Close the customizer now
  • Go to home page and you will see the search icon at right side of header menu

Now we shall implement the wp_ajax_ action into this existing search form.

Creating Ajax Nonce

For security purposes we need a ajax nonce. We shall add this nonce as a hidden input field into Astra search form. Without edit the core files of Astra theme, you can do it. There have a filter astra_get_search for search form HTML markup. You can easily alter the existing search form HTML with custom HTML markup.

Open the functions.php of your Astra child theme and add this PHP snippets:

wp_create_nonce function is creating a security nonce. I used the str_replace PHP function and added the nonce hidden input field before the </form> HTML markup.

Rest of the contents are only available to members. You must subscribe the MEMBERSHIP plan. You already subscribe the plan? Login at below.

Login

Leave a Comment





Newsletter

Get free weekly PRO Beaver Builder Tutorials

shares