Skip Navigation

[Resolved] Filter code from old site is crashing when applied to new site

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

Problem: I am trying to use some custom code from an older site in my new site, but it causes the new site to crash. The code uses a Views filter wpv_filter_taxonomy_frontend_search_get_terms_args to filter the terms included in a front-end taxonomy search filter.

Solution: Check to be sure all PHP function names are unique, and check to be sure all hooks are mapped to the correct unique function name.

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

This support ticket is created 2 years, 7 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 Pete 2 years, 6 months ago.

Assisted by: Christian Cox.

Author
Posts
#2168851

Hi there,

Toolset support a while back helped me with some code, to add to a custom code snippet to limit what was displayed in a filter.

I need to make this work on a different site however I can't get it right.

This the test page: hidden link
Click Destination in the filter bar. I would like to be able to list what is displayed.

I have tried different variations of the code below however break the site when I activate it.

This was never my strong point 🙂 Any thought please? Thank you.

// Put the code of your snippet below this comment.

add_filter( 'wpv_filter_taxonomy_frontend_search_get_terms_args', 'prefix_modify_get_terms_args', 10, 3 );

function prefix_modify_get_terms_args( $args, $taxonomy, $view_id ) {
if($view_id == 183 && $taxonomy == 'location_slug')
$args['slug'] = array(
'north-norfolk',
'hunstanton-west-norfolk',
);
return $args;
}

#2169047

Hi, I do not see anything obviously wrong in this code. I copied and pasted the same code into a site I created in my local test environment, and the site continued to function with no crashes. So there are no obvious syntax errors in the code you shared - something else must be going on.

- Can you share your site's debug information so I can see some of your configurations and plugin versions? We have information about adding debug info in our FAQ here: https://toolset.com/faq/provide-debug-information-faster-support/
- Double check to be sure you have not added more than one function called prefix_modify_get_terms_args. If you have copied and pasted this code more than once, you have probably used the function name prefix_modify_get_terms_args more than once, and this will cause a Fatal Error in PHP. So if you have multiple code snippets with copies of this same function name, you must edit each of those copies and change the function name to something unique. Here is an example of how you would use unique function names for more than one instance of this code:

add_filter( 'wpv_filter_taxonomy_frontend_search_get_terms_args', 'tssupp_modify_get_terms_args_183', 10, 3 );

function tssupp_modify_get_terms_args_183( $args, $taxonomy, $view_id ) {
if($view_id == 183 && $taxonomy == 'location_slug')
$args['slug'] = array(
'north-norfolk',
'hunstanton-west-norfolk',
);
return $args;
}

add_filter( 'wpv_filter_taxonomy_frontend_search_get_terms_args', 'tssupp_modify_get_terms_args_1234', 10, 3 );

function tssupp_modify_get_terms_args_1234( $args, $taxonomy, $view_id ) {
if($view_id == 1234 && $taxonomy == 'other_tax_slug')
$args['slug'] = array(
'term-1-slug',
'term-2-slug',
);
return $args;
}

- Can you tell me exactly how you added this code? Is it added in a custom code snippet in Toolset > Settings > Custom Code, or is it added in your child theme's functions.php file? If you can share screenshots showing the implementation of this custom code, I will take a closer look.

#2171629
aaa code s.png
aaa view.png

Hey Christian,

Thank you for getting back to me, sorry I missed your email reply the other day, only just noticed.

We have added the code via the Toolset Settings > Custom Code.

We did have another code snippet so changing the prefix_modify_get_terms_args_123
Adding the _123 ensured the site didn't break.

However this still not working. I feel I'm missing something...as mentioned we have a code snippet on another View doing a similar job.

Attached a screen of the code, this also below and the view. The code didn't work so is left 'deactivated' at the moment.

Any thoughts would be great when you have a moment.

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

add_filter( 'wpv_filter_taxonomy_frontend_search_get_terms_args', 'prefix_modify_get_terms_args', 10, 3 );

function prefix_modify_get_terms_args_123( $args, $taxonomy, $view_id ) {
if($view_id == 183 && $taxonomy == 'location_slug')
$args['slug'] = array(
'north-norfolk',
'hunstanton-west-norfolk',
);
return $args;
}

#2173273
home-page-search-update.png

We did have another code snippet so changing the prefix_modify_get_terms_args_123
Adding the _123 ensured the site didn't break....However this still not working. I feel I'm missing something...as mentioned we have a code snippet on another View doing a similar job.

Adding _123 to the function name will prevent the site from breaking, but you must also add _123 in the add_filter line as well to trigger the correct filtering function. See my screenshot here for explanation of what I mean. As you have the code written now, the function with _123 in the name is ignored, and the original function is actually called instead. Adding _123 to the filter name should help, let me know the results after updating this snippet.

#2176911

My issue is resolved now. Thank you!

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