2

I was looking through a 3rd party plugin's code today and found them doing this:

add_filter('some_base_handle'.sanitize_title($string), ...)

Curious, since I was using xh_prof to look for code that was taking up too much time and was confused why there were so many calls to sanitize title. Is it safe to remove the sanitize_title or are there good reasons for it being there?

Does anyone have links to Codex documentation that I might have missed about it? I looked at add_filter's page but didn't see anything.

EDIT:

I looked into the wp-includes/plugins.php code to see if there was anything and didn't see anything to outstanding, anyone know?

function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 )      {
    global $wp_filter, $merged_filters;

    $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority);
    $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add,     'accepted_args' => $accepted_args);
     unset( $merged_filters[ $tag ] );
    return true;
}
1
  • I looked around for problems with just php array keys and didn't find anything either, stackoverflow.com/a/9496716/1808164 though admittedly I'm not sure what keywords to really look for Commented Apr 15, 2014 at 20:35

1 Answer 1

1

add_filter('some_base_handle'.sanitize_title($string), ...)

What he's doing here is a variable filter handle, and while "sanitize_title" might not be exactly the correct function to use here, it really depends on what the possible values of $string are and what the source of that $string is.

Variable filter names can be handy if you have a lot of different things that can be filtered and you want to have some easy way to filter any/all of them.

The sanitize_title function isn't the worst to use here, because it basically formats arbitrary strings to be suitable for URLs, replacing whitespace with dashes, lowercasing everything, removing accents and the like. Maybe not the best, but again, depends on the specific case.

3
  • so there are no limits at all then? because of the way this third party code plays, it calls up these variable filters over 5000 times and costs 2 seconds to the page load for every request. I can reduce that to pretty much nothing by taking out the sanitize_title call. :) Commented Apr 15, 2014 at 20:54
  • 1
    Well, like I said, I didn't say it was necessarily a good idea, but without more specific details, I cannot say whether removing sanitize_title here will cause issues. If he's making 5k+ filter calls, then that's probably pretty bad to begin with and the issue might be deeper than using sanitize_title.
    – Otto
    Commented Apr 15, 2014 at 21:30
  • Thanks, I've removed it and haven't seen any issues from it. I could go on for miles about how bad this plugin is, but I will digress. Commented Apr 15, 2014 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.