Add Sponsored Post Prefix or Suffix to Post Titles in WordPress

Add Sponsored Post Prefix or Suffix to Post Titles in WordPress

Have you ever wondered how to get your visitors to recognize a sponsored post with a quick glance? If so, then this guide will show you a great method to add a prefix or suffix to all sponsored posts on your website. Here requires you to make some changes to custom fields and add lines of code to theme’s functions.php file.

The given coding stuff is easy to understand and use, even for beginners. Comply with the mentioned steps one by one and highlight all sponsored post titles with distinct prefix and suffix.

Add Sponsored Post Prefix in WordPress

First of all, edit a sponsored post as planned via Posts > All Posts > Your Sponsored Post. By default, there is nothing below the post content edit area, so you need to show a Custom Fields section on the screen. That is used to add meta information to current post. Move to the top of this page and click “Screen Options” to expand all options for screen customization. Target Custom Fields and check it.

Screen Options - Custom Fields

And then, a new section named as Custom Fields is shown below your post content. Move to Add New Custom Field and click “Enter New”. And now, the two blanks including Name and Value are clear to add new information for that post. Add “sponsored” to Name file while add “true” to Value field. Click “Add Custom Field” button to create the sponsored post field and then “Update” post to confirm all settings.

Add New Custom Fields

Since everything gets ready, you should set about customizing your theme’s functions.php file that can be found via Appearance > Editor. Open this file and add the following lines of code to this file. You can replace the Sponsored Post next to “sponsored_text”to any other words as you need and that will be shown as a prefix for specific sponsored post. The variables next to $post – > ID should be what you have entered in the custom fields just now.

add_filter('the_title', 'hr360_sponsored');
function hr360_sponsored($t)
{
   global $post;
   $prefix = 'Sponsored: ';
   $isSponsored = get_post_meta($post->ID, 'isSponsored', true);
   if($isSponsored == 'true' && in_the_loop())
       return $prefix.$t;
   
   return $t;
}

Click “Update File” and then check if there is a prefix displaying on the sponsored post as expected. If everything goes well, the sponsored post title should be like the following sample.

Sponsored Post Prefix

Seemingly, there is no difference between the prefix and title. To make the sponsored prefix distinct, you need to add the following code to CSS file and that is used to highlight the prefix. Go to Appearance > Editor and scroll down to the bottom to open the style.css file.

.sponsoredClass
{
    background: #eeffee;
    font-size:small;
    text-transform: uppercase;
    padding: 5px;
}

The code mentioned above allows you to customize the background, font size, text transform and padding for that prefix. Modify the parameters if needed.

Add Sponsored Post Suffix in WordPress

If you wish to add a sponsored post suffix in WordPress, then you should go with the following code. It is much the same with the code used to add sponsored post prefix. In fact, the line return $title.$sponsored_text is the key to switch the order.

add_filter( 'the_title', 'hr360_sponsored' );
function hr360_sponsored($t) 
{
   global $post;
   $suffix = 'Sponsored:  ';
   $isSponsored = get_post_meta($post->ID, 'isSponsored', true);
   if($isSponsored == 'true' && in_the_loop())
       return $t.$suffix;
   
   return $t;
}

Finally, confirm all settings and check if you have successfully add sponsored post prefix and suffix to the current sponsored post title. We hope that this guide can help you make a hit without paying much effort.