855.572.4285 support@wallit.io Log In
Premium Content

Premium Content, How to Indicate in WordPress

 

WordPress can’t be ignored – it now powers 25% of all websites with no end in sight to its growth. That’s why at iMoneza there is a strong focus on making sure our solutions can integrate and support this robust platform. We’ve heard from some of our customers that they’d like a way to indicate which items in their WordPress site are premium content. With just a simple WordPress plugin, you can accomplish this easily! Let’s see how.

Create Your WordPress Plugin

If you’re not familiar with this, please visit the official instructions for writing a WordPress plugin. After you’ve created and installed your plugin, it’s time to start adding to it. WordPress provides a number of hooks during the workflow of displaying your posts. The hook that we’re most interested in is the_title – which is what we’re going to attach the following filter to. Don’t worry – I’ll explain afterward what each line of code does.

Much like Drake, let’s start from the bottom and analyze this code. The method add_filter is what attaches our special code to this filter. The first parameter indicates the hook we’d like to apply our filter to – in this case, the generation of the title of each post. The next parameter indicates the PHP callable function we’d like to use to filter our content. The remaining arguments indicate stack and priority (see add filter documentation for more). Next, let’s look at our function itself. It receives two parameters – the title string and the ID of the post that is being filtered. First of all, the code checks to see if we’re in the admin section. We don’t need to modify the title in the admin section – in fact, doing so makes it a bit confusing actually. We only care about indicating to the reader (non-admin) that this content is premium. After the admin check, the post is checked for the premium tag – remember this, this is important for later. If the post has this tag, it adds a bit of HTML before the title. Finally, the function returns the title so that it can be displayed. What is that HTML you ask? WordPress offers a set of icons with the core that you can use to apply to your themes and plugins. These are called Dashicons. The HTML that is being generated there will be displayed with the Dashicons CSS to display a lock indicator. Now that we have this code in our plugin, we need to start marking posts as premium content.

Tag Your Content

When you’re creating your post, look for the Tags block. Tag Your Content Towards the bottom right of the standard post layout, you’ll see a section to add tags. Remember our premium tag from the code? I’ve added that to this particular post. Additionally, I’ve added another tag called test just to show that you can add more than one tag per entry. Save the post – and you’re done!

 

 

 

 

 

 

Tagged Content   Now, you’ll see some of the posts are tagged as premium, and some aren’t. It’s time to look at these on the front end with the standard 2016 theme.

 

 

 

 

 

Indicators of Premium Notice how the post we’re viewing has a lock icon indicating that it is premium. That’s because – if you remember – this particular post had the premium tag attached to it. In fact, anywhere that post titles are displayed, this filter is now applied. See how the right-hand side has some more premium items? And you’re not just limited to this very simple display. You can customize the premium indicator using your own CSS (like changing its color, making it larger, etc).

 

 

 

Styled Differently for Premium Indicator

 

 

 

 

 

Or you can even choose different dashicons for your plugin.

 

 


Wrapping Up

As you can tell, it’s pretty simple to develop a plugin to mark your posts as premium content. You might use this for other things too – prefix an article title with special avatars based on the author or the section of the website. You could even indicate posts that are galleries by putting a small gallery icon before those post titles. So, now that you know how to easily mark your posts as premium content, isn’t it time to start getting paid for it? iMoneza integrates seamlessly into WordPress allowing you to manage your content and pricing directly on the post level. And – it’s a lot easier than creating your own plugin! Contact Us to learn a bit more.

Aaron Saray

Senior Software Engineer, Open Source programmer, PHP professional, Technical Author and Editor, Javascript lover - I love popcorn, great user interfaces, and Subarus!

Leave a Reply