Please I want to achieve the goal of excluding <h2>, <a> and <img> html tag from the below code. I am trying to replace tags keywords with links within post content, and exclude the tag link from affecting the html tags i listed above.

function link_words( $text ) {
    $tags = get_tags();
    if ( $tags ) {
        foreach ( $tags as $tag ) {
            $from = '/' . $tag->name . '/';
            $to = sprintf( '<a href="%s">%s</a>', esc_url( get_term_link( $tag ) ), esc_html( $tag->name ) );
            $text = preg_replace($from, $to , $text, 2);
    return $text;
add_filter( 'the_content', 'link_words' );

Please Gurus in the house, is there any way out. I am new to wp function coding.

  • 1
    Are you certain these tags are in the post_content? Normally they are displayed with (for example) a single post template. – Bjorn Jun 28 '18 at 2:17
  • The function above, bring tags link WITHIN the post content article, not the under the post. it replaces text with link. – Godwin Alex Ogbonda Jun 28 '18 at 2:24
  • Ok, so i'm trying to understand your question... You mean the code you provided also changes the <h2> tag etc..? Why not add an unique identifier to your text based tags i.e. (curly brackets) : {tag_name}. Then do $from = '/{' . $tag->name . '}/';. – Bjorn Jun 28 '18 at 10:25
  • Please help me out, I am new to wp function. can you please write the full code ? – Godwin Alex Ogbonda Jun 28 '18 at 13:06
  • prntscr.com/k0e98w This is a screenshot of what i am referring to. there are tag link on the h2 html text. I want it to not to replace keyword that are on h2 text to tag link. it should skip it. It should only replace text that does not have hrml code wrapped to it. – Godwin Alex Ogbonda Jun 28 '18 at 13:27

Your function replaces EVERY tag_name occurence in the content. Meaning, it will replace <h2>my-tag</h2> into <h2><a href="tag-link">my-tag</a></h2>.

I'm assuming you add the tag names to the content manually. Then i would add them with an identifier. For example add the tags to your content like this:

This is my content, i can add tags that transform into links like this: {tag_name}

Then change your function into:

 * Scan post content and replace {tag_name} with tag link
add_filter( 'the_content', 'post_content_change_my_tags_into_links' );
function post_content_change_my_tags_into_links( $content ) {
  $tags = get_terms( 'post_tag' );
  if ( $tags ) {
    foreach ( $tags as $tag ) {
      $change = '{'.$tag->name.'}';
      $into = sprintf( '<a href="%s">%s</a>', esc_url( get_term_link( $tag ) ), esc_html( $tag->name ) );
      $content = str_replace($change, $into , $content);
  return $content;

NOTE 1: ONLY tags that have posts connected will transform into links.

NOTE 2: be carefull with short function names like link_words() if the function is in the global namespace (directly in functions.php). Duplicate function names will result in an error 500.

  • the code you posted did not work, it removes all the tag link from the pages entirely. – Godwin Alex Ogbonda Jun 28 '18 at 15:06

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.