Search Results for 'sf_input_object_pre function'

Forums › Forums › Search › Search Results for 'sf_input_object_pre function'

Viewing 10 results – 81 through 90 (of 115 total)
  • Author
    Search Results
  • #196334

    Anonymous
    Inactive

    Hello Team,

    No matter where I search, I can’t find a way to make the plugin run on page load.

    Once I start clicking around, the ajax filtering works fine, but I need it to work on the first page load too.

    We are using the code below to remove one of the options and to set the default active item. This all works fine.

    // Remove “All Courses” option and set main to default on Our Menu.
    add_filter( ‘sf_input_object_pre’, function ( $input_object, $sfid ) {
    if ( $sfid == 120 ) {
    // var_dump($input_object);
    if ( ( $input_object[‘name’] = ‘_sft_course’ ) ) {
    if ( empty( $input_object[‘defaults’][0] ) ) {
    $input_object[‘defaults’] = [ ‘main’ ];
    }
    $key = array_search( ‘All Courses’, array_column( $input_object[‘options’], ‘label’ ) );
    if ( $key !== false ) {
    unset( $input_object[‘options’][ $key ] );
    $input_object[‘options’] = array_values( $input_object[‘options’] );
    }
    }
    }

    return $input_object;
    }, 10, 2 );

    Can you please tell me how to instruct the plugin to run in the fresh page load?
    I can’t find the answer anywhere in the docs or internet.

    Thank you very much,
    Kota

    #195804

    Anonymous
    Inactive

    Ok I got this working! Maybe not the best way possible but it works. Here is what I have done:

    function filter_input_object($input_object, $sfid) {
       
     	if(($input_object['name']!='_sfm_colors')||($input_object['type']!='select')) {
     		return $input_object;
     	}
      
      if(!isset($input_object['options'])) {
    		return $input_object;
    	}
      
      // Move Green To Top
      $key = array_search('Green', array_column($input_object['options'], 'value'));
      $top = $input_object['options'][$key];
      unset($input_object['options'][$key]);
      array_unshift($input_object['options'], $top);
      
      // Move All Colors Back to Top
      $key1 = array_search('', array_column($input_object['options'], 'value'));
      $top1 = $input_object['options'][$key1];
      unset($input_object['options'][$key1]);
      array_unshift($input_object['options'], $top1);
      
      return $input_object;
    }
    
    add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);

    If there is a cleaner way I am interested, but this does work.

    #195760

    Anonymous
    Inactive

    No that’s not the entire code i was using, I was trying to show the filter and function I was using to try and access the object value. I have based my attempts on this github code.

    I have tried this:

    function filter_input_object($input_object, $sfid) {
       
     	if(($input_object['name']!='_sfm_colors')||($input_object['type']!='select')) {
     		return $input_object;
     	}
      
      if(!isset($input_object['options'])) {
    		return $input_object;
    	}
      
      foreach($input_object['options'] as $option) {
    	   $green = $option->value == 'Green';
      }
      
      array_splice( $input_object['options'], 1, 0, array($green) );
    	
    	return $input_object;
    }
    
    add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);

    It returns blank

    I just tried this:

    function filter_input_object($input_object, $sfid) {
       
     	if(($input_object['name']!='_sfm_colors')||($input_object['type']!='select')) {
     		return $input_object;
     	}
      
      if(!isset($input_object['options'])) {
    		return $input_object;
    	}
      
     $green = $input_object->value == 'Green';
      
     array_splice( $input_object['options'], 1, 0, array($green) );
    	
    	return $input_object;
    }
    
    add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);

    It also returns blank

    #195703

    Anonymous
    Inactive

    Yes. So for example, I am using:

    function filter_input_object($input_object, $sfid) {
     
      if(($input_object['name']!='_sfm_colors')||($input_object['type']!='select')) {
       return $input_object;
      }
     	
      if(!isset($input_object['options'])) {
        return $input_object;
      }
     
     }
     add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);

    But when I try to access a specific option, I have not been able to do so with success. I am not sure of the array structure which is part of the issue.

    #193165

    Anonymous
    Inactive

    Hi Trevor,

    This is the code we’ve used btw. Maybe it’s useful 🙂

    function filter_function_name($input_object, $sfid)
        {
            if($input_object['name']=='_sfm_kalender_datum')
            {
                foreach($input_object['options'] as $option) {
                    if($option->value != "") {
                        $date = strtotime( $option->label );
                        $option->label = date('d m Y', $date);
                    }
                }
            }
            
            return $input_object;
        }
        add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);

    Anonymous
    Inactive

    @Trevor I think I am explaining it wrong. I am not looking to change to order of the posts, I want to change the order of a specific S&F filter that I created using the S&F admin tools which is a list of checkboxes.

    This is what the field in S&F looks like

    And here is the filter on the front end of my site

    With CSS I already add icons for each item in the filter and that is the correct order. Now I need to hook in to the filter and change the order so that it is the same as in my admin, but I have no idea how to do that.

    I think I need the sf_input_object_pre filter and I already have this code

    function filter_function_name($input_object, $sfid) {
    
      if($input_object['name'] == '_sfm_category') {
        $input_object['attributes']['class'] = 'my-test-class';
        return $input_object;
      }
      return $input_object;
    }
    add_filter('sf_input_object_pre', __NAMESPACE__ . '\\filter_function_name', 10, 2);

    It took me an hour to figure out how I would get this name _sfm_category from my field category. It would be great if you could edit the docs and tell there

    If you are looking for your specific $input_object['name'] open your web inspector and look for the data attribute data-sf-field-name="_sfm_YOUR-FIELD-NAME"

    #182076

    Ross
    Keymaster

    Hi Jane

    Apologies for the delay – there’s been a good few things happenening and this slipped between the cracks.

    In regards to your original problem, you were setting the second hidden date field with JS, and it was not being picked up properly on load more / pagination.

    Well, we have a filter that allows you to set various attributes / values of each input field, via PHP (which I think may be more reliable):

    https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object

    It actually gets a bit tricky targeting the second date field, as both the fields have the same name, so I did a little test and wrote this snippet which should work (add to functions.php):

    add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);
    global $from_to_field_count;
    $from_to_field_count = 0;
    
    function filter_input_object($input_object, $sfid){
    	
    	//we need to target the second field with this name, 
    	$date_field_name = "_sfm_my_date_field";
    	
    	if( $date_field_name === $input_object['name']){
    		global $from_to_field_count;
    		$from_to_field_count++;
    		
    		if(2 === $from_to_field_count){
    			$input_object['attributes']['value'] = "29/07/2018";
    		}
    	}
    	
    	return $input_object;	
    }

    Make sure to replace _sfm_my_date_field with the name of your date field.

    Let me know how you get on!

    Thanks

    #161666

    Anonymous
    Inactive

    I solved the problem using the ‘sf_input_object_pre’ filter:

    add_filter('sf_input_object_pre', 'pas_filterlabels_aan', 10, 2);
    	function pas_filterlabels_aan($input_object, $sfid) {
    
    		if($input_object['name'] == '_sfm_vakdomein') {
    			foreach($input_object['options'] as $option) {
    				$term_id = $option->value;
    				if ($option->value) {
    					$term = get_term_by('id', $term_id, 'cursussoort');
    					$term_label = $term->name;
    					$option->label = $term_label.' ('.$option->count.')';
    				}
    
    			}
    			
    		}
    	
    	return $input_object;	
    	}

    Replace ‘_sfm_vakdomein’ by your input object name (is shown in the query string) and ‘cursussoort’ by your taxonomy slug.

    #160225

    Anonymous
    Inactive

    Hi, I’m trying to wrap <li> items in the filter in a div so I can easily apply an accordion to them. I’ve been using sf_input_object_pre to modify the prefix and postfix but they don’t seem to insert anything into the HTML – can someone confirm what these attributes do? And if they don’t insert any prefix or postfix, is there a way I can add HTML to the filter list?
    Thanks.

    function filter_function_name($input_object, $sfid) {
    
    	if($input_object['type']!='checkbox') 	{
    		return $input_object;
    	}	
    	foreach($input_object['options'] as $key => $option) {
    		if ($option->attributes['class']="sf-level-0") {
    		      $input_object['options'][$key]->prefix = '<div>';
                          $input_object['options'][$key]->postfix= '</div>';
    		}
    	}
    	
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);
    #152293

    Anonymous
    Inactive

    OK Here it is. I needed to show the author name by last, first, and sometimes the username by last,first. So I have a hook (which yes, you put in functions.php) and I check the form id and the field we’re outputting, and process it based on what the values are.

    If we’re looking at the author field, i basically requery the entire author list, and regenerate the list completely. You can see I exclude some authors (like admin, and some other random people i didn’t want in the list)

    If it’s the user field, i do a loop and requery the user and then adjust the info.

    I think i wrote the two if statements at different times, because the second way could probably be applied to the first. Whatever, it works.

    function sf_edit_author_field_order($input_object, $sfid)
    {
    	if( in_array( $sfid, array( 3390, 3444, 3562 ) ) && $input_object['name'] == '_sf_author' ) {
            // requery the author list
            $author_args = array(
                'exclude_admin' => true,
                'hide_empty' => true,
                'meta_key' => 'last_name',
                'orderby' => 'meta_value',
                'order' => 'ASC',
                'exclude' => array( 429, 233, 558, 7 )
            );
            $user_query = new WP_User_Query( $author_args );
            $users = $user_query->get_results();
            $new_users = array();
            $new_users[] = (object) array(
                'label' => 'All Authors',
                'attributes' => array (
                    'class' => 'sf-level-0 sf-item-0'
                ),
                'value' => '',
                'count' => 0
            );
            // go through users and get their user_nicename and display_name
            foreach( $users as $user ) {
                $new_users[] = (object) array(
                    'attributes' => array(
                        'class' => 'sf-level-0'
                    ),
                    'value' => $user->user_nicename,
                    'label' => $user->last_name . ', ' . $user->first_name,
                    'count' => 1
                );
            }
            $input_object['options'] = $new_users;
    	} else if ( $sfid == 3444 && $input_object['name'] == '_sft_userlist' ) {
            // requery the tagged user list to get their names - last, first
            // go through the options part of the object and get the user id, and then the meta info for that
            foreach ($input_object['options'] as $option) {
                // get the user id from the term meta
                $temp_term = get_term_by( 'slug', $option->value, 'userlist');
                $temp_term->meta_user_id = get_term_meta( $temp_term->term_id, 'user_id', true );
                $last_name = get_user_meta( $temp_term->meta_user_id, 'last_name', true );
                $first_name = get_user_meta( $temp_term->meta_user_id, 'first_name', true );
                // overwrite the original label
                if ( $last_name && $first_name ) {
                    $option->label = $last_name . ', ' . $first_name;
                }
            }
        }
    	
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'sf_edit_author_field_order', 10, 2);
Viewing 10 results – 81 through 90 (of 115 total)
Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.