How to Add Sponsored Post Prefix to Post Title in WordPress

Would you like to automatically add a ‘Sponsored Post’ prefix to blog post titles in WordPress?

Once you start publishing sponsored posts, you need to disclose which posts are sponsored. You could do this by adding the word ‘Sponsored’ to the post title or by adding a disclaimer in the body of the post.

In this article, we will show you how to automatically add a ‘Sponsored Post’ prefix to your post titles in WordPress.

How to Add Sponsored Post Prefix to Post Title in WordPress

Why Add Sponsored Post Prefix to Your Posts?

Once your WordPress blog starts to attract more traffic, you may start thinking about how to monetize it. You can use WordPress and blogging to make money online by doing what you love.

One way to do that is through sponsorships. This is where a company pays you to represent their product, talk about it, and promote it to your readers. The sponsorship may be for a single post or an ongoing relationship.

When publishing sponsored posts, it’s crucial to know about the laws in your area about disclosure.

For example, in the United States, a blogger who publishes a sponsored post must comply with the FTC’s Endorsement Guides. This includes disclosing whenever a post is sponsored.

One way to do that is to add a prefix to the title of every sponsored post.

Preview of Sponsored Post Preview with Custom CSS

We’ll show you how to do exactly that in this guide. It does involve the use of code snippets and custom fields, which may seem intimidating to beginners, but we’ll walk you through every step.

In this article, we will be using a custom field to add a ‘sponsored’ custom field to a post. This will give us a way of identifying which posts are sponsored and which aren’t.

Custom fields can be used to add any information related to the post, page, or any content type. This meta-information can be displayed in your theme.

To add the ‘Sponsored Post’ prefix to your titles, you’ll need to edit your WordPress theme files. We’ll do that by adding a custom code snippet to your theme’s functions.php file.

With that being said, let’s take a look at how to add a ‘Sponsored Post’ prefix to your post titles in WordPress.

How to Add a Sponsored Post Custom Field to Your Post

Start by opening one of your existing sponsored posts or creating a new one. The custom fields panel is normally located below the content editor. However, if you’ve never used custom fields before, then it will be hidden.

The way to display the custom fields metabox depends on whether you are using the block editor or classic editor.

If you use the block editor, then you need to click on the three-dot menu at the top-right corner of the screen and select ‘Preferences’ from the menu.

Block Editor Preferences

This will bring up a Preferences popup where you need to go to the Panels section and then turn on the ‘Custom fields’ switch. After that, click on the ‘Enable & Reload’ button to reload the post editor.

Display Custom Fields in Block Editor

If you are still using the old WordPress classic editor, then you need to click the ‘Screen Options’ button on the top right corner of your screen. This will open a menu where you need to check the box next to custom fields options.

Screen Options in Classic Editor

The post editor will reload, and when you scroll below the content editor you’ll be able to see the custom fields panel.

Add Custom Field

To add a new custom field, just type ‘sponsored’ in the Name text box, and ‘true’ in the Value text box.

Adding a New Custom Field

Next, you need to save your post and scroll down to custom fields meta box. You will notice that custom field meta box is now showing a drop down menu.

Next time you write a sponsored post, all you need to do is select ‘sponsored’ from the drop down menu and enter ‘true’ in the Value field.

Custom Field Drop Down Menu

How to Add the Sponsored Post Prefix with a Code Snippet

After adding the sponsored custom field to your post, you need to add the code that will display the sponsored post title prefix.

To do that, you need to copy and paste this code snippet into your theme’s functions.php file or a site-specific plugin.

add_filter( 'the_title', 'wpb_sponsored' );
function wpb_sponsored( $title ) {
   global $post;
   $sponsored_text = ' ';
   $sponsored = get_post_meta($post->ID, 'sponsored', true);
   if( $sponsored == 'true' && in_the_loop() ){
       return $sponsored_text.$title;
   }
   return $title;
}

In this tutorial, we’re using the free Code Snippets plugin to add the code to our site. You can see more details in our tutorial on how to add code snippets to WordPress.

Code Snippet to Display Sponsored Post Prefix

Once you’ve saved and activated the snippet, WordPress will start displaying the prefix. Try visiting the post you edited earlier, and you will see ‘Sponsored Post’ prefixed to the post title.

Preview of Sponsored Post Prefix

We’re making great progress, and with a bit more work we can improve the appearance of the prefix.

If you study the code snippet, then you will notice that we have wrapped the sponsored post text around a CSS class we named ‘sponsored_text’. You can use this CSS class to highlight the text inside the post title.

Here is a little CSS that you can add to your theme or child theme’s stylesheet. If you’re new to CSS, then be sure to check out our guide on how to easily add custom CSS to your WordPress site.

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

 
Preview of Sponsored Post Preview with Custom CSS

The code makes the background of the text green and uppercase and makes the font size small. It also adds a little padding around the highlight. Feel free to modify the CSS to meet your needs.

How to Add a Sponsored Post Suffix with a Code Snippet

You might want to add the words ‘Sponsored Post’ to the end of your post’s title instead of the beginning. You can achieve that by using this code snippet instead.

add_filter( 'the_title', 'wpb_sponsored' );
function wpb_sponsored( $title ) {
   global $post;
   $sponsored_text = ' ';
   $sponsored = get_post_meta($post->ID, 'sponsored', true);
   if( $sponsored == 'true' && in_the_loop() ){
       return $title.$sponsored_text;
   }
   return $title;
}

 
Custom Code Snippet for Sponsored Post Suffix

When you study the code you’ll notice that we’ve made just two changes. We added a single letter space before the sponsored text, and then we switched the order to display $title first.

Preview of Sponsored Post Suffix with Custom CSS

We hope this article helped you to learn how to add a sponsored post prefix to post titles in WordPress. You might also like to learn the right way to create an email newsletter or see our comparison of the best email marketing services.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.