Useful WordPress Hooks – Part 2


In our second episode of Useful WordPress Hooks, we are going to explore more into the world of WordPress hooks. If you have missed the first part, read here about Useful WordPress Hooks – Part 1

3. Automatically insert content after posts that contains a specific tag

Depending on the structure of the website and requirements, sometimes it requires to append some call to action blocks at the end of the each post content. But posts that contains specific tags for example Financial Advice.

In this case, we can make use of the the_content action filter that helps us to ineract with the post content. We will make the $post variable global to access post specific data e.g. post ID. So we can pull up some other necessary data like what tags are used on this post.

Based on that, we can check if the post contains a specific tag. If yes, then we can insert any HTML code to display whatever we want. Let’s look at the code example below.

 * Insert content after post
 * @params string $content
 * @return string $content
function insert_content_after_post( $content ) {
  global $post;

  // Check if the current post has the "education" tag
  $finance_advice_tag = get_term_by('slug', 'finance-advice', 'post_tag');
  // return if the tag not found
  if (!$finance_advice_tag) {
    return $content;
  $has_finance_advice_tag = has_term($finance_advice_tag->term_id, 'post_tag', $post);

  // If the post has the "finance-advice" tag, insert your desired content after the post
  if ($has_finance_advice_tag) {
      $additional_content = '<div class="additional-content">'. __('Your financial advice banner in here', 'site-textdomain') .'</div>';
      $content .= $additional_content;

  return $content;
add_filter( 'the_content', 'insert_content_after_post' );

Using the same method, we can target for different tags to display different call to action blocks for each tags and category.


Leave a Reply

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