Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andrewlimaza/2fd37bda89ae295b833a3cf39e4488f6 to your computer and use it in GitHub Desktop.
Save andrewlimaza/2fd37bda89ae295b833a3cf39e4488f6 to your computer and use it in GitHub Desktop.
Show icon based on level access and posts
<?php
/**
* This will add an image to the top of each post based on if a user has access to that post or not.
* Add the code below to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function show_icon_based_on_membership_access( $hasaccess, $thepost, $theuser, $post_membership_levels ) {
if ( ! empty( $post_membership_levels ) ) {
// check if the user has access for a restricted post.
if ( $hasaccess ) {
add_filter( 'the_content', 'my_content_filter_members' );
} else {
// let's filter the content message
add_filter( 'the_content', 'my_content_filter_non_members' );
}
}
return $hasaccess;
}
add_filter( "pmpro_has_membership_access_filter", "show_icon_based_on_membership_access", 10, 4 );
function my_content_filter_non_members( $content ) {
$content = '<img src="link-to-icon-for-members" />' . $content;
return $content;
}
function my_content_filter_members( $content ) {
$content = '<img src="link-to-icon-for-members" />' . $content;
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment