Skip Navigation

[Resolved] Sanitize text filter input in WordPress Archive before submit a search

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a custom field filter (text input) in my WordPress Archive. I would like to sanitize the User's input in this field programmatically before search. For example, I would like to remove any URL querystring parameters added to the URL and search for the main URL without any URL parameters.

Solution: Use the API wpv_filter_custom_field_filter_processed_value to manipulate the User's search criteria programmatically before search. You can use this filter to inspect the URL and truncate it before any querystring parameters. Use the Wordpress Archive ID instead of the View ID.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_custom_field_filter_processed_value

This support ticket is created 3 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by WeiS2074 3 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#1728819
sanitizer.JPG

Hi,

In a custom type archive, there is a custom field textbox, which hook should I use to modify the textbox value before searching?

#1729623

Hi we have two filters available for processing custom field filter values - one for the original value, and another for the processed value after passing through the check for URL params, shortcode attributes and date functions.:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_custom_field_filter_original_value
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_custom_field_filter_processed_value

Both of those links include code samples if you'd like to see how to use these hooks. Click the orange "+More Usage examples" button to see those samples.

Let me know if you have questions about these, or if I misunderstood what you want to accomplish.

#1730699

1. Where do I find my view_id? This is my archive, do I use the Toolset->Wordpress Archives->ID?
2. I use the following code, it doesn't work. Where does it go wrong?
3. My website is live. So I create another instance from my backup, then I work from there. Of course they have different IP, but under the archive page, all the custom post isn't displayed. If I click on each post, it was there and show on my screen. But it just don't show any record in the archive page.

function remove_url_tracking( $value, $key, $view_id){
if ( $key == 'wpcf-cf-url' ) {
$pos = strpos($value, '?');
if ($pos > 1) {
$value = substr($value, 0, $pos);
}
//$value = $url_santinized;
}
return $value;
}
add_filter( 'wpv_filter_custom_field_filter_original_value', 'remove_url_tracking', 99, 3 );

#1732427

1. Where do I find my view_id? This is my archive, do I use the Toolset->Wordpress Archives->ID?
Yes, use the archive ID instead of the View ID.

2. I use the following code, it doesn't work. Where does it go wrong?
If you want to filter the value of the filter text, you should use the second API, wpv_filter_custom_field_filter_processed_value. Check the examples in the documentation. The first API is for a preprocessed value like URL_PARAM(wpcf-cf-url). The second API is for the processed value, like an actual URL.

3. My website is live. So I create another instance from my backup, then I work from there. Of course they have different IP, but under the archive page, all the custom post isn't displayed. If I click on each post, it was there and show on my screen. But it just don't show any record in the archive page.
I'm not sure, can you provide more information?
- Have you resaved your site permalinks?
- Have you tried with a different archive with no filters or custom code? Please create a new WordPress Archive with no Query Filters and temporarily apply this new WordPress Archive to this custom post type. Do the results appear as expected in this new temporary archive?

#1735869

2. it works now.
3. If I change to an archive which has no filter, it works. And today I use the original archive, it still works.

Thank you.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.