Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Created August 27, 2020 04:58
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 ipokkel/0bdc4d4533acb4a814accfb56cb6e3f5 to your computer and use it in GitHub Desktop.
Save ipokkel/0bdc4d4533acb4a814accfb56cb6e3f5 to your computer and use it in GitHub Desktop.
Include access to restricted pages with Limit Post View but exclude a specific page from being viewed.
<?php
/**
* This recipe provides access on a limited basis to a specific page
* using Limit Post Views Add On.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
// Add 'pages' for Limit Post Views Add On for Paid Memberships Pro.
add_filter( 'pmprolpv_post_types', 'my_pmprolpv_reset_post_types', 10, 1 );
function my_pmprolpv_reset_post_types( $post_types ) {
// set Limit Post Views to work only with pages.
$post_types = array();
$post_types[] = 'page';
return $post_types;
}
/*
Restrict page view access to a specific page
*/
add_filter( 'pmprolpv_has_membership_access', 'my_pmprolpv_restrict_page_id', 10, 2 );
function my_pmprolpv_restrict_page_id( $hasaccess, $post ) {
// If access to pages is possible, restrict a specific page from LPV access
if ( is_page( 2 ) ) { // replace the number with your page id
$hasaccess = false;
}
return $hasaccess;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment