Skip to content

Instantly share code, notes, and snippets.

@kimcoleman
Created October 27, 2023 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimcoleman/adacd984ade3cd416e2cbdbc415274b3 to your computer and use it in GitHub Desktop.
Save kimcoleman/adacd984ade3cd416e2cbdbc415274b3 to your computer and use it in GitHub Desktop.
An example template that uses the pmpro_has_membership_access function to protect content.
<?php
/**
* Template Name: My Custom Template
*
* An example template that uses the pmpro_has_membership_access function to protect content.
*
*/
get_header(); ?>
<main id="main" class="site-main" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php
// Check if the user has access.
$hasaccess = pmpro_has_membership_access( $post->ID, null, true );
if ( ! empty( $hasaccess[0] ) ) {
// The user has access, show the custom template content or page content.
echo $post->post_content;
} else {
// If the user doesn't have access, show the pmpro_get_no_access_message.
echo pmpro_get_no_access_message( '', $hasaccess[1], $hasaccess[2] );
}
?>
<?php endwhile; ?>
</main><!-- #main -->
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment