Last active
August 29, 2015 14:08
-
-
Save luistinygod/2df4dfa67c7f384e7333 to your computer and use it in GitHub Desktop.
GravityView Make (some) Search Fields Required (custom plugin)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: GravityView - Custom Required Search Fields | |
Plugin URI: https://gravityview.co/ | |
Description: Custom Add-on to make certain GravityView Search fields required before performing the search | |
Author: Katz Web Services, Inc. | |
Version: 1.0.0 | |
Author URI: http://www.katzwebservices.com | |
Copyright 2014 Katz Web Services, Inc. (email: info@katzwebservices.com) | |
This program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation; either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
// tested with GravityView version 1.3 | |
/** | |
* If you need to make some of the GravityView search fields required before search is submited | |
* - add as many lines as you need to define which fields are required | |
* e.g. -> jQuery('#search-box-filter_XYZ').prop('required', true ); | |
* | |
*/ | |
add_action('gravityview_after', 'gv_custom_make_search_fields_required' ); | |
function gv_custom_make_search_fields_required() { | |
?> | |
<script> | |
//define here the search fields that are required | |
jQuery('#search-box-filter_2').prop('required', true ); | |
jQuery('#search-box-filter_1_3').prop('required', true ); | |
jQuery('#search-box-filter_1_6').prop('required', true ); | |
// validation magic | |
jQuery('form.gv-widget-search').submit( function(e) { | |
e.stopPropagation(); | |
var isValid = true; | |
jQuery('.gv-widget-search input:required').each( function(){ | |
if( jQuery(this).val() === '' ) { | |
isValid = false; | |
} | |
}); | |
return isValid; | |
}); | |
// hide search results if search wasn't performed yet | |
jQuery(document).ready(function() { | |
if( jQuery('form.gv-widget-search').length > 0 ) { | |
var gvSearchAction = jQuery('form.gv-widget-search').attr('action'); | |
if( gvSearchAction.indexOf('?') === -1 ) { | |
jQuery('.gv-container').hide(); | |
} else { | |
jQuery('.gv-container').show(); | |
} | |
} | |
}); | |
</script> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment