Skip to content

Instantly share code, notes, and snippets.

@ronalfy
Created October 27, 2020 14:59
Show Gist options
  • Save ronalfy/22e7139c6d7d9ea3797e337464d27914 to your computer and use it in GitHub Desktop.
Save ronalfy/22e7139c6d7d9ea3797e337464d27914 to your computer and use it in GitHub Desktop.
PMPro - Allow Access to Certain Series Non-Members
<?php
/**
* Allow non-logged-in users access to certain series.
*
* 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/
*/
/**
* Allow access to certain series for non-logged-in-members.
*/
function my_pmpros_pmpro_has_membership_access_filter( $hasaccess, $post, $user, $post_membership_levels ) {
if ( is_user_logged_in() ) {
return $hasaccess;
}
$series_ids_to_allow = array(
875,
18662,
);
$post_id = get_queried_object_id();
if ( get_post_type( $post_id ) == 'pmpro_series' ) {
return true;
}
$maybe_in_series = get_post_meta( $post_id, '_post_series', true );
if ( ! $maybe_in_series || ! is_array( $maybe_in_series ) || empty( $maybe_in_series ) ) {
return $hasaccess;
}
foreach ( $series_ids_to_allow as $series_id ) {
if ( in_array( $series_id, $maybe_in_series ) ) {
return true;
}
}
return $hasaccess;
}
add_filter( 'pmpro_has_membership_access_filter', 'my_pmpros_pmpro_has_membership_access_filter', 15, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment