Created
October 27, 2020 14:59
-
-
Save ronalfy/22e7139c6d7d9ea3797e337464d27914 to your computer and use it in GitHub Desktop.
PMPro - Allow Access to Certain Series Non-Members
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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