Skip to content

Instantly share code, notes, and snippets.

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/aff54541a0089822cab79581eb97168b to your computer and use it in GitHub Desktop.
Save ipokkel/aff54541a0089822cab79581eb97168b to your computer and use it in GitHub Desktop.
<?php
/**
* Allow admins to access posts for specific membership levels.
*
* 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/
*/
function pmpro_allow_access_for_admins_for_set_levels( $hasaccess, $post, $user, $post_membership_levels ) {
// Set the admin membership levels here.
$admin_levels = array( 1, 5, 7 ); // Add the level IDs that should have access here.
// Skip if the user already has access.
if ( $hasaccess ) {
return $hasaccess;
}
// Get level IDs.
$post_membership_levels_ids = array_map( 'intval', wp_list_pluck( $post_membership_levels, 'id' ) );
// Allow access if user is an admin and post level matches set admin levels.
if ( current_user_can( 'manage_options' ) && ! empty( $post_membership_levels_ids ) && array_intersect( $admin_levels, $post_membership_levels_ids ) ) {
$hasaccess = true;
}
return $hasaccess;
}
add_filter( 'pmpro_has_membership_access_filter', 'pmpro_allow_access_for_admins_for_set_levels', 30, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment