add_filter hook not working

Home Forums Calendar Products Filter Bar add_filter hook not working

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #922353
    GDPR20191021
    Participant

    Hi,
    I’m trying to add a custom field on filter bar and in this forum you suggest to use the “add_filter(‘tribe_events_all_filters_array’, ‘custom_add_filter_callback’);” hook.
    But this hook doesn’t work. Has there been maybe any changes since the 3.9.0 version?
    Thanks in advance!

    #922425
    Brian
    Keymaster

    Hi,

    Sorry for the issues you are having with the filter tribe_events_all_filters_array.

    It is still in the plugin in 3.9.

    Here are two topics that might help out:

    https://theeventscalendar.com/support/forums/topic/adding-custom-filter-to-filter-bar/

    https://theeventscalendar.com/support/forums/topic/adding-a-custom-filter/

    Let me know if you have any follow up questions.

    Thanks

    #922437
    GDPR20191021
    Participant

    Hi Brian,
    I’ve already seen these topics and tried to copy/paste and integrate in functions.php (like any other hooks), but it doesn’t work for me. Here’s my code:

    add_filter(‘tribe_events_all_filters_array’, ‘custom_add_filter_callback’);
    public function custom_add_filter_callback($filters) {
    $filters[‘eventcomplete’] = array(
    ‘name’ => ‘Complete’,
    ‘type’ => ‘checkbox’,
    ‘admin_form’ => ‘custom-field[_2]’,
    );
    return $filters;
    }

    Thanks in advance!

    #922444
    GDPR20191021
    Participant

    I’m sorry, I did a mistake. Here’s the right code:

    add_filter(‘tribe_events_all_filters_array’, ‘custom_add_filter_callback’);
    function custom_add_filter_callback($filters) {
    $filters[‘eventcomplete’] = array(
    ‘name’ => ‘Complete’,
    ‘type’ => ‘checkbox’,
    ‘admin_form’ => ‘custom-field[_2]’,
    );
    return $filters;
    }

    #922446
    GDPR20191021
    Participant

    What could be the issue?

    #922510
    Brian
    Keymaster

    Unfortunately, it is not a simple hook to add in data it is more like BuddyPress Tabs where you have to use the filter with a function to show the information.

    The method in the Filter Bar to create a fitler looks like this:

    public function allFiltersArray( $filters ) {
    $this_filter = array(
    'name' => $this->name,
    'type' => $this->type,
    'admin_form' => $this->get_admin_form(),
    );

    If you look at this file:

    \the-events-calendar-filterbar\lib\filters\TribeEventsFilter_Category.php

    You can see how the categories filter is created and that would have to be something similar to what you are looking to do.

    I can try to answer questions about it, but we are unable to provide all the coding for a customization and would not be able to troubleshoot the entire process with you.

    Thanks

    #935093
    GDPR20191021
    Participant

    Hi folks,

    for those who payed for and nevertheless didn’t get a valuable help, here is my solution:

    Connect to your DB, open the options table (the name should be something like “wp_options”, depending on your prefix) and select the the row where “option_name” is “tribe_events_calendar_options”.
    Search your additional field to find out it’s name. The name should be something like _ecp_custom_{X}, i.e. in my case: _ecp_custom_6.

    Go to folder:
    /wp-content/plugins/the-events-calendar-filterbar/lib/filters/

    And create a file, in my case:
    TribeEventsFilter_District.php

    Add this content:

    
    <?php
    
    /**
     * Class TribeEventsFilter_District
     */
    class TribeEventsFilter_District extends TribeEventsFilter {
    	
    	/**
    	 * Additional fields are stored in DB in table "options".
    	 * The "option_name" is "tribe_events_calendar_options".
    	 * Their option values are stored under their respective name "_ecp_custom_{X}",
    	 * "_ecp_custom_{X}" being the slug for this filter and {X} being the number (position in array) needed to retrieve the information.
    	 */
    	protected function get_ecp_custom_number(){
    		preg_match_all('!\d+!', $this->slug, $matches);
    		$result = implode('', $matches[0]);
    		return (int)$result;
    	}
    	
    	/**
    	 * Fictive name for this filter to use in SQL query.
    	 */
    	protected function generate_custom_filter_join_name(){
    		return 'custom_filter_'.$this->slug;
    	}
    	
    	/**
    	 * New line sometimes stays at the end of a value.
    	 */
    	protected function escape_new_line($string){
    		$result = $string;
    		$result = str_replace("\r", '', $result);
    		$result = str_replace("\n", '', $result);
    		return $result;
    	}
    	
    	public function get_admin_form() {
    		$title = $this->get_title_field();
    		$type = $this->get_type_field();
    		return $title.$type;
    	}
    	
    	protected function get_type_field() {
    		$name = $this->get_admin_field_name('type');
    		
    		$field = sprintf( __( 'Type: %s %s %s %s', 'tribe-events-filter-view' ),
    			sprintf( '<label><input type="radio" name="%s" value="select" %s /> %s</label>',
    				$name,
    				checked( $this->type, 'select', false ),
    				__( 'Dropdown', 'tribe-events-filter-view' )
    			),
    			sprintf( '<label><input type="radio" name="%s" value="radio" %s /> %s</label>',
    				$name,
    				checked( $this->type, 'radio', false ),
    				__( 'Radio Buttons', 'tribe-events-calendar-pro' )
    			),
    			sprintf( '<label><input type="radio" name="%s" value="checkbox" %s /> %s</label>',
    				$name,
    				checked( $this->type, 'checkbox', false ),
    				__( 'Checkboxes', 'tribe-events-filter-view' )
    			),
    			sprintf( '<label><input type="radio" name="%s" value="autocomplete" %s /> %s</label>',
    				$name,
    				checked( $this->type, 'autocomplete', false ),
    				__( 'Autocomplete', 'tribe-events-filter-view' )
    			)
    		);
    		return '<div class="tribe_events_active_filter_type_options">'.$field.'</div>';
    	}
    		
    	protected function get_values() {
    		$options = get_option( 'tribe_events_calendar_options', false );
    		$options_this_filter = $options['custom-fields'][$this->get_ecp_custom_number()];
    		
    		$array_values = explode(PHP_EOL, $options_this_filter['values']);
    
    		$values = array();
    		foreach($array_values as $value){
    			$value = $this->escape_new_line($value);
    			$values[] = array(
    				'name' =>  __( $value, 'tribe-events-filter-view' ),
    				'value' => $value,
    			);
    		}
    
    		return $values;
    	}
    		
    	protected function setup_join_clause() {
    		global $wpdb;
    		$custom_filter_join_name = $this->generate_custom_filter_join_name();
    		$this->joinClause = " LEFT JOIN {$wpdb->postmeta} AS {$custom_filter_join_name} ON ({$wpdb->posts}.ID = {$custom_filter_join_name}.post_id)";
    	}
    	
    	protected function setup_where_clause() {
    		global $wpdb;
    		$custom_filter_join_name = $this->generate_custom_filter_join_name();
    				
    		$clauses = array();
    		$sql_prepare = "({$custom_filter_join_name}.meta_key = '".$this->slug."' AND {$custom_filter_join_name}.meta_value = %s AND {$custom_filter_join_name}.meta_value IS NOT NULL) ";
    		if (is_array($this->currentValue)){
    			foreach ( $this->currentValue as $value ){
    				$clauses[] = $wpdb->prepare($sql_prepare, $value);
    			}
    		}
    		else{
    			    $clauses[] = $wpdb->prepare($sql_prepare, $this->currentValue);
    		}
    		
    		$this->whereClause = ' AND ('.implode(' OR ', $clauses).') ';
    	}
    
    }
     
     

    Open file:
    /wp-content/plugins/the-events-calendar-filterbar/lib/tribe-filter-view.class.php

    Go to function:
    initialize_filters

    And add your filter, taking care of your filter’s name, in my case it’s “_ecp_custom_6”:

    
    new TribeEventsFilter_District( __( 'District', 'tribe-events-filter-view' ), '_ecp_custom_6');
    

    That’s it! Hope it helps.

    #935169
    Brian
    Keymaster

    Hi,

    Glad you figured it out and thank you for sharing the coding.

    Sorry we are unable to help more and did not meet your expectations on customization help. We try to help out on every customization about 10 to 15 minutes, but we are unable to help out on entire customizations as much as we like too.

    Per our terms of service you are buying the plugin with support to get the features running as advertised, but not access to development help, although we still try to provide some it is not what you are paying for when you purchase the plugin.

    Thanks again for providing the coding.

    I am going to close this ticket, but if you need anything else related to this topic or another please post a new topic on the forum and we can help you out.

    Thanks

Viewing 8 posts - 1 through 8 (of 8 total)
  • The topic ‘add_filter hook not working’ is closed to new replies.