Show LOCK icon by protected posts titles

Note: This is an advanced article for users or developers familiar with PHP, WordPress hooks, and FontAwesome.

If you'd like to show a LOCK icon next to your protected posts title's in your blog or search or archive views, you can do so by following the instructions below.

First you'll want to install a free plugin called Code Snippets (if you don't already have it).

Once installed create a new Snippet and paste the following into it

function mepr_show_lock_icon($title, $post_id) {
  $post = get_post($post_id);

  if(!class_exists('MeprRule')) { return $title; }

  if(is_admin() || defined('REST_REQUEST')) { return $title; }

  if(!isset($post->ID) || !$post->ID) { return $title; }

  if(strpos($title, 'fa-lock') !== false) { return $title; } //Already been here?

  if(MeprRule::is_locked($post)) {
    $title = '<i class="fa fa-lock" aria-hidden="true"></i>' . " {$title}";
  }

  return $title;
}
add_filter('the_title', 'mepr_show_lock_icon', 1000, 2);

function enqueue_mepr_font_awesome() {
  wp_enqueue_style('mepr-font-awesome', 'https://memberpress-font-awesome.s3.amazonaws.com/css/font-awesome.min.css'); 
}
add_action('wp_enqueue_scripts','enqueue_mepr_font_awesome');

Before clicking save, be sure to select "Front End" for the code snippet as shown below

Then "Save Changes and Activate" the snippet.

That's it! You're protected posts should now show a lock icon before their title on your blog.

TROUBLESHOOTING:

If it's not working, you may need to check your browsers developer tools to see if any errors appear in the console.

It's possible that your theme or another plugin on the site is already using font-awesome causing a conflict. If that's the case, you can delete lines #20-23 and see if it works any better.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.