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 Answer
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.

Leave a Reply

Your email address will not be published. Required fields are marked *