apply_filters( ‘the_tags’, string $tag_list, string $before, string $sep, string $after, int $post_id )

Filters the tags list for a given post.

Parameters

$tag_liststring
List of tags.
$beforestring
String to use before the tags.
$sepstring
String to use between the tags.
$afterstring
String to use after the tags.
$post_idint
Post ID.

Source

return apply_filters( 'the_tags', $tag_list, $before, $sep, $after, $post_id );

Changelog

VersionDescription
2.3.0Introduced.

User Contributed Notes

  1. Skip to note 3 content

    The_tags receives an html string formed in this way:

    $html = "<a href="http://my_website.local/tag/tag_one/&quot; rel="tag">tag_one</a>, <a href="http://my_website.local/tag/tag_two/&quot; rel="tag">tag_two</a>";

    In order to replace a tag you may use Clase DOMDocument. For example, you can replace all tags using a replacement word. but do not change the tag’s links. A more useful example can replace some tags by an alias or by a translation.

    add_filter( 'the_tags', 'my_filter_the_tags');
    function my_filter_the_tags( $html)
    {
      $dom = new DOMDocument();
      $dom->loadHtml($html);
      $anchors = $dom->getElementsByTagName('a');
      $nb = $anchors->length;
     
      for($pos=0; $pos<$nb; $pos++)
      {
        $anchors->item($pos)->nodeValue = 'any_replacement';
       // check error_log( $anchors->item($pos)->nodeValue); 
      }
       
      $html = $dom->saveHTML();
      return $html;
       
    }

    output is this:

    <a href="http://my_website.local/tag/tag_one/&quot; rel="tag">any_replacement</a>, <a href="http://my_website.local/tag/tag_two/&quot; rel="tag">any_replacement</a>
  2. Skip to note 4 content

    An easiest way to replace the_tags

    add_filter( 'the_tags', 'my_filter_the_tags');
    function my_filter_the_tags( $html)
    {
    	$tags = get_tags();
    	$out = '';
    	foreach ( $tags as $key=>$tag)
            {
                $pos = strpos( $html , $tag->name );
                if (  $pos > 0 ) 
    			{
       
                    $replace = my_tag_translation($tag->name);
                    $out .= '<a href = "'.site_url() .'/tag/' .  $tag->slug . ' " rel="tag" title=""  >' .  $replace . ' ,</a>  ';
               	}
            }
    }

You must log in before being able to contribute a note or feedback.