Excellent Programming Tricks

Stop empty search in WordPress using JavaScript

Use below code in js file (suppose jq_e.js)
$(function(){
 $(document).ready(function() {
  /* SEARCH FIELD */
  $('#searchform').submit(function(e) { // submit function pin when event occur
  var s = $( this ).find("#s");
  
  if (s.val() == "") {
   e.preventDefault(); // stop default submission
   alert("ERROR! Search Field is Empty.");
   $('#s').focus();
  }
 });
});
 Use below code in function.php
function my_search_form( $form ) {
 $form = '<fieldset><legend>Search:</legend>
  <form id="searchform" class="searchform" action="' . home_url( '/' ) . '" method="get">
   <div><label class="screen-reader-text" for="s">' . "" . '></label>
        <input id="s" name="s" type="text" value="' . get_search_query() . '" placeholder="Please Text Your Search Item..." />
        <input id="searchsubmit" type="submit" value="'. esc_attr__( 'Search...' ) .'" />
   </div>
  </form>
<fieldset>';

return $form;
}
add_filter( ‘get_search_form’, ‘my_search_form’ );

function wp_enqueue_function() {
       wp_register_script(‘ig_jquery’, esc_url(get_template_directory_uri()).’/js/ig_jquery.js’);
      wp_enqueue_script(‘ig_jquery’);
}
Also, your search template file may be in search.php file and you can use below code.
/*
Template Name: Search
*/
<div class="search_box"> 
<?php 
 get_search_form();       
?>
</div> <!-- end of search box -->




No comments:

Post a Comment


Authentic аnd Excellent

Website

HTML Template

Wordpress Theme

Database applications

OR

Application services?

Excellent Programming Tricks (EPT) || Iftekhar-IT || We develops the Web applications and the WordPress templates. || Excellent Programming Tricks (EPT)

© 2020 Blogger Theme by Iftekhar IT || Excellent Programming Tricks

Execllent Programming Tricks. Powered by Blogger.