Skip to content

Instantly share code, notes, and snippets.

@dikiyforester
Last active July 17, 2018 14:08
Show Gist options
  • Save dikiyforester/59371e7ac3235989499faf6bd5717e05 to your computer and use it in GitHub Desktop.
Save dikiyforester/59371e7ac3235989499faf6bd5717e05 to your computer and use it in GitHub Desktop.
Replace ClassiPress Location Search field with States dropdown
<?php
/**
* The listing search form template file
*
* @package ClassiPress
* @since 4.0.0
*/
if ( ! isset( $search_form_location ) ) {
$search_form_location = 'bar';
}
$is_widget = 'widget' === $search_form_location;
$location_search = false;
$states_field_token = '';
$form_fields = cp_get_custom_form_fields();
$states_field = wp_list_filter( $form_fields, array( 'field_name' => 'cp_state' ) );
if ( ! empty( $states_field ) ) {
$location_search = true;
$states_field = array_shift( $states_field );
$select_options = cp_explode( ',', $states_field->field_values );
$html_options = '';
$html_options .= html( 'option', array( 'value' => '' ), __( 'All States', APP_TD ) );
$cp_state = '';
if ( ! empty( $_GET['cp_state'] ) && is_array( $_GET['cp_state'] ) ) {
$cp_state = $_GET['cp_state'];
$cp_state = array_shift( $cp_state );
}
foreach ( $select_options as $option ) {
$args = array( 'value' => $option );
if ( ( $option === $cp_state ) || ( esc_attr( $option ) === $cp_state ) ) {
$args['selected'] = 'selected';
}
$html_options .= html( 'option', $args, $option );
}
$args = array( 'name' => $states_field->field_name . '[]', 'id' => $states_field->field_name );
$states_field_token = html( 'select', $args, $html_options );
}
?>
<script type="text/javascript">
jQuery( function( $ ) {
$('.search-form').submit( function () {
$( this )
.find('.search-state-wrap select')
.filter( function () {
return !this.value;
} )
.prop( 'name', '' );
});
} );
</script>
<form method="get" class="search-form" action="<?php echo esc_url( get_post_type_archive_link( APP_POST_TYPE ) ); ?>" role="search">
<div class="row">
<div class="search-keywords-wrap medium-<?php echo $is_widget ? 12 : ( $location_search ? 4 : 5 ); ?> columns">
<input name="s" type="search" id="search_keywords" class="search_keywords" value="<?php the_search_query(); ?>" placeholder="<?php echo esc_attr_x( 'What are you looking for?', 'placeholder text', APP_TD ); ?>" />
</div>
<?php if ( $location_search ) { ?>
<div class="search-state-wrap medium-<?php echo $is_widget ? 12 : 3; ?> columns">
<?php echo $states_field_token; ?>
</div><!-- .search-state-wrap -->
<?php } ?>
<div class="search-category-wrap medium-<?php echo $is_widget ? 12 : ( $location_search ? 3 : 5 ); ?> columns">
<?php wp_dropdown_categories( cp_get_dropdown_categories_search_args( $search_form_location ) ); ?>
</div>
<div class="search-button-wrap medium-<?php echo $is_widget ? 12 : 2 ?> columns">
<button type="submit" class="button expanded">
<i class="fa fa-search" aria-hidden="true"></i>
<?php esc_html_e( 'Search', APP_TD ); ?>
</button>
</div>
<?php
/**
* Fires after the main header search fields.
*
* @since 4.0.0
*/
do_action( 'cp_listing_header_search_fields_after' );
?>
<input type="hidden" name="lat" value="<?php echo ! empty( $_GET[ 'lat' ] ) ? esc_attr( $_GET[ 'lat' ] ) : 0; ?>">
<input type="hidden" name="lng" value="<?php echo ! empty( $_GET[ 'lng' ] ) ? esc_attr( $_GET[ 'lng' ] ) : 0; ?>">
<input type="hidden" name="st" value="<?php echo esc_attr( APP_POST_TYPE ); ?>">
<input type="hidden" name="refine_search" value="yes">
</div> <!-- .row -->
</form>
@dikiyforester
Copy link
Author

Add this file to the root directory of the child theme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment