Halloween Sale 15% off Ends in See the plans
PHP

Lock Icon on Members-Only Post Titles

Adds a small lock symbol before the titles of members-only posts in lists and archives for visitors, so they know what needs an account before clicking.

Don’t have WPIDE yet? Install it on your site

Code

$wpide_lock_category = 'members';

add_filter( 'the_title', function ( $title, $post_id = 0 ) use ( $wpide_lock_category ) {
    if ( is_user_logged_in() || ! $post_id || is_admin() || ! in_the_loop() || is_singular() && get_queried_object_id() === (int) $post_id ) {
        return $title;
    }

    $locked = get_post_meta( $post_id, '_wpide_members_only', true ) || ( 'post' === get_post_type( $post_id ) && has_category( $wpide_lock_category, $post_id ) );

    return $locked ? '<span class="wpide-lock" aria-label="Members only" role="img">🔒</span> ' . $title : $title;
}, 10, 2 );

Notes

Works with posts marked members-only by the WPIDE "Members-Only Posts" snippet or placed in the members category; edit $wpide_lock_category to match yours.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.