WPBeginner

Beginner's Guide for WordPress

  • Blog
    • Beginners Guide
    • News
    • Opinion
    • Showcase
    • Themes
    • Tutorials
    • WordPress Plugins
  • Start Here
    • How to Start a Blog
    • Create a Website
    • Start an Online Store
    • Best Website Builder
    • Email Marketing
    • WordPress Hosting
  • Deals
    • Bluehost Coupon
    • SiteGround Coupon
    • WP Engine Coupon
    • HostGator Coupon
    • Domain.com Coupon
    • Constant Contact
    • View All Deals »
  • Glossary
  • Videos
  • Products
X
☰
Beginner's Guide for WordPress / Start your WordPress Blog in minutes
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

WPBeginner» Blog» Tutorials» How to Add Sponsored Post Prefix to Post Title in WordPress

How to Add Sponsored Post Prefix to Post Title in WordPress

Last updated on August 12th, 2014 by Editorial Staff
52 Shares
Share
Tweet
Share
Pin
Special WordPress Hosting offer for WPBeginner Readers
How to Add Sponsored Post Prefix to Post Title in WordPress

Often you see bloggers publish sponsored posts on their blog. Recently one of our users asked if it was possible to automatically add a “Sponsored” prefix in post titles. In this article, we will show you how to add sponsored post prefix to post title in WordPress.

Sponsored Post prefix added to post title in WordPress

Adding Sponsored Post Prefix Using Custom Fields

Custom fields allow you to add meta information to your posts. In this article we will be using custom fields to add sponsored meta field to a post.

First you need to create a new post or edit an existing one. In the post editor, scroll down to the custom fields metabox. If you can not see the custom fields meta box in your post edit area, then you need to click on the Screen Options button on the top right corner of the screen. This will open a menu where you need to check the box next to custom fields options.

Making custom fields meta box visible in WordPress post editor

Now scroll down to the custom fields meta box and add sponsored in the custom field Name, and true in the value field.

Adding sponsored custom field in the custom fields meta box

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 need to set a sponsored post, all you need to do is select sponsored from the drop down menu and enter true in the value field.

After adding the sponsored custom field to your post, 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 = '<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;
}

That’s all. Try visiting the post you edited earlier, and you will see Sponsored Post: prefix with the post title.

If you study the code snippet, you will notice that we have wrapped sponsored post text around a CSS class. Using this CSS class you can highlight the text inside the post title. Here is a little CSS that you can add to your theme or child theme‘s stylesheet.

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

Feel free to modify the CSS to meet your needs.

Adding Sponsored Post Suffix to Post Title in WordPress

In case you want to display the sponsored post text after the post title, then you can achieve this by using this code snippet:

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;
}

If you study the code we have made just two changes. We have added a single letter space before the sponsored text, and then we have switched the order to display $title first.

That’s all, we hope this article helped you add a sponsored post prefix / suffix to post title in WordPress.

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

52 Shares
Share
Tweet
Share
Pin
Popular on WPBeginner Right Now!
  • Error Establishing a Database Connection in WordPress

    How to Fix the Error Establishing a Database Connection in WordPress

  • Google Analytics in WordPress

    How to Install Google Analytics in WordPress for Beginners

  • Checklist

    Checklist: 15 Things You MUST DO Before Changing WordPress Themes

  • How to Properly Move Your Blog from WordPress.com to WordPress.org

About the Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Trusted by over 1.3 million readers worldwide.

The Ultimate WordPress Toolkit

11 Comments

Leave a Reply
  1. kenn says:
    Apr 6, 2019 at 7:37 pm

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

    Reply
    • WPBeginner Support says:
      Apr 8, 2019 at 12:00 pm

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

      Reply
  2. vo thang says:
    Aug 29, 2018 at 9:25 am

    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.

    Reply
  3. Angela Scott says:
    Sep 10, 2016 at 5:34 pm

    Is there a way to add an image in place of the text, please?

    Reply
  4. Soibibo says:
    Aug 21, 2016 at 9:56 pm

    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.

    Reply
    • WPBeginner Support says:
      Aug 22, 2016 at 12:04 am

      Please take a look at our guide on pasting snippets from web into WordPress.

      Reply
  5. Ranish Malhan says:
    Apr 15, 2015 at 8:56 am

    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?

    Reply
  6. WPBeginner Staff says:
    Nov 27, 2014 at 2:46 pm

    Yes.

    Reply
  7. VIAINA says:
    Nov 26, 2014 at 4:33 pm

    Hi, does “sponsored post” is also shown in the RSS feed?
    Thanks.

    Reply
  8. clreed87 says:
    Aug 13, 2014 at 11:13 am

    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?

    Reply
  9. Caroline says:
    Aug 13, 2014 at 9:43 am

    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?

    Reply

Leave a Reply Cancel 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.

Over 1,320,000+ Readers

Get fresh content from WPBeginner

Featured WordPress Plugin
WPForms Logo
WPForms
Drag & Drop WordPress Form Builder Plugin. Learn More »
How to Start a Blog How to Start a Blog
I need help with ...
Starting a
Blog
WordPress
Performance
WordPress
Security
WordPress
SEO
WordPress
Errors
Building an
Online Store
Useful WordPress Guides
    • 7 Best WordPress Backup Plugins Compared (Pros and Cons)
    • How to Fix the Error Establishing a Database Connection in WordPress
    • Why You Need a CDN for your WordPress Blog? [Infographic]
    • 25 Legit Ways to Make Money Online Blogging with WordPress
    • Self Hosted WordPress.org vs. Free WordPress.com [Infograph]
    • Free Recording: WordPress Workshop for Beginners
    • 24 Must Have WordPress Plugins for Business Websites
    • How to Properly Move Your Blog from WordPress.com to WordPress.org
    • 5 Best Contact Form Plugins for WordPress Compared
    • Which is the Best WordPress Popup Plugin? (Comparison)
    • Best WooCommerce Hosting in 2019 (Comparison)
    • How to Fix the Internal Server Error in WordPress
    • How to Install WordPress - Complete WordPress Installation Tutorial
    • Why You Should Start Building an Email List Right Away
    • How to Properly Move WordPress to a New Domain Without Losing SEO
    • How to Choose the Best WordPress Hosting for Your Website
    • How to Choose the Best Blogging Platform (Comparison)
    • WordPress Tutorials - 200+ Step by Step WordPress Tutorials
    • 5 Best WordPress Ecommerce Plugins Compared
    • 5 Best WordPress Membership Plugins (Compared)
    • 7 Best Email Marketing Services for Small Business (2019)
    • How to Choose the Best Domain Registrar (Compared)
    • The Truth About Shared WordPress Web Hosting
    • When Do You Really Need Managed WordPress Hosting?
    • 5 Best Drag and Drop WordPress Page Builders Compared
    • How to Switch from Blogger to WordPress without Losing Google Rankings
    • How to Properly Switch From Wix to WordPress (Step by Step)
    • How to Properly Move from Weebly to WordPress (Step by Step)
    • Do You Really Need a VPS? Best WordPress VPS Hosting Compared
    • How to Properly Move from Squarespace to WordPress
    • How to Register a Domain Name (+ tip to get it for FREE)
    • HostGator Review - An Honest Look at Speed & Uptime (2019)
    • SiteGround Reviews from 1032 Users & Our Experts (2019)
    • Bluehost Review from Real Users + Performance Stats (2019)
    • How Much Does It Really Cost to Build a WordPress Website?
    • How to Create an Email Newsletter the RIGHT WAY (Step by Step)
    • 7 Best CRM Software for Small Businesses (Compared)
    • How to Create a Free Business Email Address in 5 Minutes (Step by Step)
    • How to Install Google Analytics in WordPress for Beginners
    • How to Move WordPress to a New Host or Server With No Downtime
    • Why is WordPress Free? What are the Costs? What is the Catch?
    • How to Make a Website in 2019 – Step by Step Guide
Deals & Coupons (view all)
Weglot Coupon
Get 15% OFF on Weglot multilingual plugin for WordPress.
Webnus
Webnus Coupon
Get 20% OFF on Webnus themes and plugins for WordPress.
Featured In
About WPBeginner®

WPBeginner is a free WordPress resource site for Beginners. WPBeginner was founded in July 2009 by Syed Balkhi. The main goal of this site is to provide quality tips, tricks, hacks, and other WordPress resources that allows WordPress beginners to improve their site(s).

Site Links
  • About Us
  • Contact Us
  • FTC Disclosure
  • Privacy Policy
  • Terms of Service
  • Free Blog Setup
Our Sites
  • OptinMonster
  • MonsterInsights
  • WPForms
  • SeedProd
  • Nameboy
  • RafflePress

Copyright © 2009 - 2019 WPBeginner LLC. All Rights Reserved. WPBeginner® is a registered trademark.

Managed by Awesome Motive | WordPress hosting by HostGator | WordPress CDN by MaxCDN | WordPress Security by Sucuri.