Problem: I have a custom search View with several search fields placed on a page. Only the search form, not the results, are shown on the page. The View is set up to redirect to a different URL, where the filters and results are both displayed. When Users visit the first page, the URL will have a parameter "promocode" with some variable. When the page redirects to the next step, I want to maintain the original URL parameter on the results page.
Solution: Use the View filter "wpv_filter_end_filter_form" to add a hidden input field to the custom search form, and set the value of the field by accessing the URL parameter in the $_GET superglobal:
add_filter( 'wpv_filter_end_filter_form', 'prefix_add_hidden_field', 99, 4 ); function prefix_add_hidden_field( $out, $view_settings, $view_id, $is_required ) { $views = array( 16855 ); if ( in_array( $view_id, $views) && $is_required ) { $promocode = isset($_GET['promocode']) ? $_GET['promocode'] : ''; $out = '<input type="hidden" id="promocode" name="promocode" value="' . $promocode . '" />' . $out; } return $out; };
Then if your View is set up to "only show available inputs" you'll need to add some custom JavaScript to update the hidden promocode field any time the filters are updated:
jQuery( document ).on( 'js_event_wpv_parametric_search_form_updated', function( event, data ) { var url = new URL(window.location); var promocode = url.searchParams.get("promocode"); jQuery('#promocode').val(promocode); });
Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_end_filter_form
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 11 replies, has 2 voices.
Last updated by 6 years, 1 month ago.
Assisted by: Christian Cox.