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_list

(string) List of tags.

$before

(string) String to use before the tags.

$sep

(string) String to use between the tags.

$after

(string) String to use after the tags.

$post_id

(int) Post ID.


Top ↑

Source

File: wp-includes/category-template.php

View on Trac



Top ↑

Changelog

Changelog
Version Description
2.3.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by capbussat

    The_tags receives an html string formed in this way:

    $html = "<a href="http://my_website.local/tag/tag_one/" rel="tag">tag_one</a>, <a href="http://my_website.local/tag/tag_two/" 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/" rel="tag">any_replacement</a>, <a href="http://my_website.local/tag/tag_two/" rel="tag">any_replacement</a>
  2. Skip to note 2 content
    Contributed by capbussat

    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.