Created
July 23, 2024 09:21
-
-
Save dwanjuki/e1d393e0ad506acacc8f724324160269 to your computer and use it in GitHub Desktop.
Custom no access message that includes membership level names
This file contains 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 | |
/** | |
* Custom "no access" message that includes membership level names | |
* | |
* 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 my_pmpro_custom_no_access_message_body( $body, $required_level_ids ) { | |
$pmpro_levels = pmpro_getAllLevels(); | |
$required_level_count = sizeof( $required_level_ids ); | |
$required_level_names = array(); | |
foreach( $pmpro_levels as $level_id => $level ) { | |
if( in_array( $level_id, $required_level_ids ) ) { | |
$required_level_names[] = $level->name; | |
} | |
} | |
if ( $required_level_count > 1 ) { | |
$levels_string = implode( ', ', array_slice( $required_level_names, 0, $required_level_count - 1 ) ) . ' and ' . end( $required_level_names ); | |
} else { | |
$levels_string = $required_level_names[0]; | |
} | |
$body = "This content is for {$levels_string} members only."; | |
return $body; | |
} | |
add_filter( 'pmpro_no_access_message_body', 'my_pmpro_custom_no_access_message_body', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment