I am having some difficulties trying to strip out special characters and spaces from my tags. Using the below code:

                <ul class="work_tags_nav">
                <li><a class="all" href="#">all.</a></li>
                query_posts(array( 'post_type'=> 'work' ));
                if ( have_posts() ) : while ( have_posts() ) : the_post();
                    $custom_post_tags = get_the_tags();
                if ( $custom_post_tags ) {
                    foreach( $custom_post_tags as $tag ) {
                        $tags_arr[] = $tag -> name;
                endwhile; endif;
                if( $tags_arr ) {
                    $uniq_tags_arr = array_unique( $tags_arr );
                 foreach( $uniq_tags_arr as $tag ) {
                     $sanitizeTag =  sanitize_title($tag);
                     $tag_link = get_term_by('name', $tag, 'post_tag');                                              
                     echo '<li><a class="'.$tag.'" href="javascript:void(0)">' .$tag. '.</a></li>';

On this line: echo '<li><a class="'.$tag.'" href="javascript:void(0)">' .$tag. '.</a></li>';

I want the class="" to show the tag but if the tag has "&" or spaces etc. to strip them out. Any ideas?


Here it how you can do it.

$tag = 'medical &amp; health';
$tag_class = preg_replace('/[^A-Za-z0-9-]+/', '-', html_entity_decode($tag));
echo '<li><a class="'.$tag_class.'" href="javascript:void(0)">' .$tag. '.</a></li>';
  • M-R, still seems to be rendering out "&" in the class. one of my tags is Medical & Health, but the class is still rendering: <li><a href="javascript:void(0)" class="medical &amp; health">medical &amp; health.</a></li> ideally id like it to be rendered as medical_health or medical-health I guess. Thank you! – Danny Jul 31 '13 at 16:13
  • 1
    Please see the updated answer. You can change second parameter of preg_replace from dash '-' to underscore '_' – M-R Jul 31 '13 at 16:27

You can also use a built-in function: sanitize_title();. For more info see here.

//returns medical-health
echo sanitize_title( 'Medical & Health' );

Even easier (and faster) with native PHP functions:

$tag_name = filter_var( $tag_name, FILTER_SANITIZE_SPECIAL_CHARS );

To obtain post_tag links use get_tag_link(), if it is for a different taxonomy, use get_term_link() instead.

You can as well use get_the_tag_list() or get_the_term_list() to build a list of taxonomy taxons (terms, catagories, post tags).

This would look close to the following as there's one filter:

apply_filters('wp_get_object_terms', $terms, $object_ids, $taxonomies, $args);

And the callback would look something like that:

add_filter( 'wp_get_object_terms', 'wpse108491_term_links' );
function wpse108491_term_links( $terms, $object_ids, $taxonomies, $args )
    if ( is_admin() )
        return $links;

    remove_filter( current_filter(), __FUNCTION__ );

    foreach ( $terms as $k => $t )
        $terms[ $k ]->name = filter_var( $t->name, FILTER_SANITIZE_SPECIAL_CHARS  );

    return $links

Then simply call it like this:

echo get_the_term_list(

Keep in mind that above callback isn't tested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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