WordPress.org

Support

Support » Plugins and Hacks » Events Manager » [Plugin: Events Manager] Add custom search field to events-search.php

[Plugin: Events Manager] Add custom search field to events-search.php

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Marcus

    @netweblogic

    if you check templates/templates/em-search.php you could copy the state/city section and modify it to use ‘town’ instead, it should just ‘work’ once you get that showing up correctly.

    yup. this somehow works.

    BUT you also have to change em-actions.php to get the correct AJAX city search.

    in events-search.php

    <select name="town" class="em-events-search-state">
    					<option value=''><?php _e('All Cities','dbem'); ?></option>
    					<?php
    					if( !empty($country) ){
    						//get the towns from locations table
    						global $wpdb;
    						$em_states = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT location_town FROM ".EM_LOCATIONS_TABLE." WHERE location_town IS NOT NULL AND location_town != '' AND location_country=%s", $country), ARRAY_N);
    						foreach($em_states as $state){
    							?>
    							 <option <?php echo ($_REQUEST['state'] == $state[0]) ? 'selected="selected"':''; ?>><?php echo $state[0]; ?></option>
    							<?php
    						}
    					}
    					?>
    				</select>

    plus the javascript (replace all javascript with following):

    <script type="text/javascript">
    	jQuery(document).ready( function($){
    		$('.em-events-search-form select[name=country]').change( function(){
    			$('.em-events-search select[name=town]').html('<option><?php _e('Loading...','dbem'); ?></option>');
    			$('.em-events-search select[name=region]').html('<option><?php _e('Loading...','dbem'); ?></option>');
    			var data = {
    				_wpnonce : '<?php echo wp_create_nonce('search_towns'); ?>',
    				action : 'search_towns',
    				country : $(this).val(),
    				return_html : true
    			};
    			$('.em-events-search select[name=town]').load( EM.ajaxurl, data );
    			data.action = 'search_regions';
    			data._wpnonce = '<?php echo wp_create_nonce('search_regions'); ?>';
    			$('.em-events-search select[name=region]').load( EM.ajaxurl, data );
    		});
    
    		$('.em-events-search-form select[name=region]').change( function(){
    			$('.em-events-search select[name=town]').html('<option><?php _e('Loading...','dbem'); ?></option>');
    			var data = {
    				_wpnonce : '<?php echo wp_create_nonce('search_towns'); ?>',
    				action : 'search_towns',
    				region : $(this).val(),
    				country : $('.em-events-search-form select[name=country]').val(),
    				return_html : true
    			};
    			$('.em-events-search select[name=town]').load( EM.ajaxurl, data );
    		});
    
    		//in order for this to work, you need the above classes to be present in your theme
    		$('.em-events-search-form').submit(function(){
    	    	if( this.search.value=='<?php echo $s_default; ?>'){
    	    		this.search.value = '';
    	    	}
    	    	if( $('#em-wrapper .em-events-list').length == 1 ){
    				$(this).ajaxSubmit({
    					url : EM.ajaxurl,
    				    data : {
    						_wpnonce : '<?php echo wp_create_nonce('search_towns'); ?>',
    						action : 'search_towns',
    						country : $(this).val(),
    						return_html : true
    					},
    				    beforeSubmit: function(form) {
    						$('.em-events-search-form :submit').val('<?php _e('Searching...','dbem'); ?>');
    				    },
    				    success : function(responseText) {
    						$('.em-events-search-form :submit').val('<?php _e('Search','dbem'); ?>');
    						$('#em-wrapper .em-events-list').replaceWith(responseText);
    				    }
    				});
    	    	}
    		});
    	});
    </script>

    em-actions.php
    line 477 – 503

    //AJAX call for searches
    	// JNZ31 CHANGED FROM STATE TO TOWN f.ex. : 'search_states' becomes 'search_towns'
    	if( !empty($_REQUEST['action']) && substr($_REQUEST['action'],0,6) == 'search' ){
    		if( $_REQUEST['action'] == 'search_towns' && wp_verify_nonce($_REQUEST['_wpnonce'], 'search_towns') ){
    			$results = array();
    			if( !empty($_REQUEST['country']) ){
    				$results = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT location_town AS value, location_country AS country, CONCAT(location_town, ', ', location_country) AS label FROM " . EM_LOCATIONS_TABLE ." WHERE location_town IS NOT NULL AND location_town != '' AND location_country=%s", $_REQUEST['country']));
    			}elseif( !empty($_REQUEST['region']) ){
    				$results = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT location_town AS value, location_country AS country, CONCAT(location_town, ', ', location_country) AS label FROM " . EM_LOCATIONS_TABLE ." WHERE location_town IS NOT NULL AND location_town != '' AND location_region=%s", $_REQUEST['region']));
    			}
    			if( $_REQUEST['return_html'] ) {
    				//quick shortcut for quick html form manipulation
    				ob_start();
    				?>
    				<option value=''><?php _e('All Cities','dbem'); ?></option>
    				<?php
    				foreach( $results as $result ){
    					echo "<option>{$result->value}</option>";
    				}
    				$return = ob_get_clean();
    				echo apply_filters('em_ajax_search_towns', $return);
    				exit();
    			}else{
    				echo EM_Object::json_encode($results);
    				exit();
    			}
    		}

    please note:
    i for myself kicked the state-search so if you want to keep it, you have to mod this stuff as well..

    Plugin Author Marcus

    @netweblogic

    thx for contributing

    evielutions

    @evielutions

    Yes yes yes, so excited it works!!! Thanks much!!

    there is one little error in it, and i dont know how to solve it. maybe someone can help:
    when switching the country, the city select box gets properly updated.
    but when switching back to all countries, the city select box is empty.

    rontarson

    @rontarson

    Yes. I see that same error of switching back to All Countries, although in my application the country would never be anything than the US.

    I also have the error that if I plug in a town to search, the search does not return any values, even though I know it should. Just region search works, but when throwing in a town it stops working. Thoughts?

    looks like this is all obsolete in 5.1.3
    nice <3

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘[Plugin: Events Manager] Add custom search field to events-search.php’ is closed to new replies.
Skip to toolbar