Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save andrewlimaza/415a992de7e7a4cb532edfb9aa339973 to your computer and use it in GitHub Desktop.

Select an option

Save andrewlimaza/415a992de7e7a4cb532edfb9aa339973 to your computer and use it in GitHub Desktop.
Show "Premium" next to the title tag for posts in Paid Memberships Pro.
<?php
/**
* Append a "Premium" badge to post titles on archive/blog/search pages
* when the post requires a PMPro membership level.
*/
function my_pmpro_premium_badge_on_title( $title, $post_id = null ) {
if ( is_singular() || is_admin() ) {
return $title;
}
if ( ! function_exists( 'pmpro_has_membership_access' ) ) {
return $title;
}
$post_levels = pmpro_has_membership_access( $post_id, null, true );
// pmpro_has_membership_access returns [ bool $hasaccess, array $post_levels, array $user_levels ]
// We want to show the badge when the post has *any* required level (regardless of current user access).
if ( empty( $post_levels[1] ) ) {
return $title;
}
$badge = '<span class="pmpro-premium-badge" aria-label="' . esc_attr__( 'Members only', 'paid-memberships-pro' ) . '">'
. '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" aria-hidden="true" focusable="false" fill="currentColor" width="1em" height="1em"><path d="M12 1L9 9H2l6 4.5L5.5 21 12 17l6.5 4L16 13.5 22 9h-7z"/></svg>'
. esc_html__( 'Premium', 'paid-memberships-pro' )
. '</span>';
return $title . ' ' . $badge;
}
add_filter( 'the_title', 'my_pmpro_premium_badge_on_title', 10, 2 );
/**
* Styles for the premium badge.
*/
function my_pmpro_premium_badge_styles() {
if ( is_singular() || is_admin() ) {
return;
}
?>
<style>
.pmpro-premium-badge {
display: inline-flex;
align-items: center;
gap: 0.25em;
font-size: 0.6em;
font-weight: 600;
letter-spacing: 0.05em;
text-transform: uppercase;
color: #fff;
background: #b8860b;
border-radius: 3px;
padding: 0.2em 0.5em;
vertical-align: middle;
line-height: 1.4;
white-space: nowrap;
}
.pmpro-premium-badge svg {
flex-shrink: 0;
}
</style>
<?php
}
add_action( 'wp_head', 'my_pmpro_premium_badge_styles' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment