Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

How to Add Sponsored Post Prefix to Post Title in WordPress

Editorial Note: We earn a commission from partner links on WPBeginner. Commissions do not affect our editors' opinions or evaluations. Learn more about Editorial Process.

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

Once you start publishing sponsored posts, you must 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 meta box depends on whether you are using the block editor or the 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 then reload.

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.

In the next field, be sure that the Value is ‘true.’

Adding a New Custom Field

Next, you need to save your post and scroll down to the custom fields meta box. You will notice that the 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 add a code snippet to your website. Normally, this is done through your theme’s functions.php file.

However, we don’t recommend editing your theme’s functions.php directly because the smallest error can break your site. Plus, if you update your WordPress theme, all of your customizations will be removed.

Instead, we recommend the WPCode, the easiest and safest way to add code to your WordPress site without having to edit your theme’s functions.php file.

WPCode WordPress code snippets plugin

To get started, you need to install and activate the free WPCode plugin. For step by step instructions, see our guide on how to install a WordPress plugin.

Once the plugin is activated, head to Code Snippets » + Add Snippet from your WordPress admin dashboard. Then, hover over the ‘Add Your Custom Code (New Snippet)’ option and click the ‘Use snippet’ button.

Add your new custom code snippet in WPCode

After that, simply copy and paste the code snippet into the code box.

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

Don’t forget to choose the ‘PHP Snippet’ option from the Code Type drop-down list on the right side of the screen.

Add Code Snippet to Display Sponsored Post Prefix in WPCode

Click the ‘Inactive’ toggle to change it to ‘Active’ and then click the Save Snippet button.

Now 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.

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

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.

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 = '<span class="sponsored_text"> Sponsored Post</span> ';
   $sponsored = get_post_meta($post->ID, 'sponsored', true);
   if( $sponsored == 'true' && in_the_loop() ){
       return $title.$sponsored_text;
   }
   return $title;
}
Add Custom Code Snippet for Sponsored Post Suffix in WPCode

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.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

12 CommentsLeave a Reply

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. kenn says

    how can i created sponsored products not post. i want my sellers to be able to create sponsored products

    • WPBeginner Support says

      You would want to check with the support for your ecommerce plugin for if it has a sponsored product option

      Admin

  3. vo thang says

    I’ve done it,that’s Ok.But,Title post is appear in home page without sponsored text.

    How can I make it appear in Home page ?

    Thank you so much.

  4. Soibibo says

    Hi, I’m new to WordPress and talking about pasting codes to function.php file almost made me loose my site. please where exactly can I post this code on the function.php folder? I have zero knowledge of codes.

  5. Ranish Malhan says

    Hi,
    I have created a custom meta feild for my custom post type which is a check box.
    So can u please tell how to display posts by checking whether that checkbox is checked or not?

  6. clreed87 says

    Thanks for the great tip! Will filtering on the_title() also be reflected in the RSS feeds for sponsored posts or just on the blog posts on the site?

  7. Caroline says

    Great article. Works like a charm. But I cannot seem to make the custom field be saved so I can pick it from the dropdown box for a next post.
    Any idea what I’m doing wrong?

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.