• Resolved emirpprime

    (@emirpprime)


    Can anyone provide a complete example use of custom form fields?

    I have added my fields to the form using simple_locator_form_fields(), and got them sent over ajax using simple_locator_post_fields(), but I can’t work out how to then utilise those fields… I presume the idea is to use them in simple_locator_sql_where() but I don’t see that you can actually access the custom fields as nothing is passed in to the filter.

    Anyone got this worked out??

    Thanks!

Viewing 1 replies (of 1 total)
  • Thread Starter emirpprime

    (@emirpprime)

    Ah – sorted this myself by directly accessing $_POST. In case it helps anyone else:

    /**
     * Filter Simple Locator shortcode Form output to add extra fields.
     *
     * @param  string $output     String of form html to be output.
     * @param  array  $distances  Values for distances dropdown.
     * @param  [type] $taxonomies [description]
     * @param  bool   $widget     Whether output is for the widget.
     * @return string             Modified $output.
     */
    function mysite_form_bootstrap( $output, $distances, $taxonomies, $widget ) {
    	if ( true === $widget ) {
    		return $output;
    	}
    
    	// Extra inputs.
    	$extras = '<div class="form-check"><label class="form-check-label"><input name="public" class="form-check-input" type="checkbox" value="true" checked> Show public events only</label></div>';
    	$extras .= '<div class="form-check"><label class="form-check-label"><input name="upcoming" class="form-check-input" type="checkbox" value="true" checked> Show upcoming events only</label></div>';
    	$output = str_replace( '<div class="submit">', $extras . '<div class="submit">', $output );
    
    	return $output;
    }
    add_filter( 'simple_locator_form', 'mysite_form_fields', 10, 4 );
    
    /**
     * Filter Simple Locator form handling to make it send our additional fields.
     *
     * @return string Additional fields to add to post data.
     */
    function mysite_form_post_fields() {
    	return array( 'upcoming', 'public' );
    }
    add_filter( 'simple_locator_post_fields', 'mysite_form_post_fields' );
    
    /**
     * Join custom fields meta for use in Select.
     *
     * @param  string $sql Join statment to select meta fields.
     * @return string      Modified join statment to select meta fields.
     */
    function mysite_additional_fields_join( $sql ){
    	return $sql . ' LEFT JOIN wp_postmeta AS public_or_private
    			ON p.ID = public_or_private.post_id AND public_or_private.meta_key = "is_your_event_public_or_private"';
    }
    add_filter( 'simple_locator_sql_join', 'mysite_additional_fields_join' );
    
    /**
     * Update Simple Locator Where SQL to handle filter checkboxes.
     * @param  string $sql Where SQL statement to select events.
     * @return string      Modified where SQL statement to select events.
     */
    function mysite_additional_fields_where( $sql ){
    
    	$public = filter_input( INPUT_POST, 'public', FILTER_SANITIZE_STRING );
    	if ( ! empty( $public ) && 'true' === $public ) {
    		$sql .= ' AND public_or_private.meta_value = "Public"';
    	}
    
    	$upcoming = filter_input( INPUT_POST, 'upcoming', FILTER_SANITIZE_STRING );
    	if ( ! empty( $upcoming ) && 'true' === $upcoming ) {
    		$sql .= ' AND date_of_your_event.meta_value >= "' . date( 'Ymd' ) . '"';
    	}
    	return $sql;
    }
    add_filter( 'simple_locator_sql_where', 'mysite_additional_fields_where' );

    NB – in this specific case I also had to use JS to modify the checkbox value depending on whether they were checked or not as Simple Locator doesn’t have any extra handling for showing whether they are checked or not in the POSTed data.

    • This reply was modified 6 years, 11 months ago by emirpprime.
    • This reply was modified 6 years, 11 months ago by emirpprime.
Viewing 1 replies (of 1 total)
  • The topic ‘Help with simple_locator_post_fields()’ is closed to new replies.