Instantly share code, notes, and snippets.

Embed
What would you like to do?
Access denied messages for RSS feeds and Non Member users. Custom message based on the membership level the user _does_ have
<?php
/**
* Plugin Name: Paid Memberships Pro - Custom Advanced Settings Messages
* Plugin URI: https://eighty20results.com/paid-memberships-pro/do-it-for-me/
* Description: Per membership level message for the membership/login required
* Version: 1.0
* Author: Paid Memberships Pro / Thomas Sjolshagen <thomas@eighty20results.com>
* Author URI: https://eighty20results.com/thomas-sjolshagen/
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* License: GPLv2 or later
*
* Copyright (c) 2017 - Eighty / 20 Results by Wicked Strong Chicks.
* ALL RIGHTS RESERVED
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Return the membership level specific text for non-members
*
* @param string $text
*
* @return string
*/
function pca_non_member_text_filter_by_level( $text ) {
global $current_user;
$user_level = pmpro_getMembershipLevelForUser( $current_user->ID );
if ( isset( $user_level->id ) ) {
return pca_get_level_messages( 'non_member', $user_level->id );
}
return $text; // Returning the default message (from Advanced Settings page)
}
add_filter( 'pmpro_non_member_text_filter', 'pca_non_member_text_filter_by_level', 10, 1 );
/**
* Return the membership level specific text for RSS feeds
*
* @param string $text
*
* @return string
*/
function pca_rss_text_filter_by_level( $text ) {
global $current_user;
$user_level = pmpro_getMembershipLevelForUser( $current_user->ID );
if ( isset( $user_level->id ) ) {
return pca_get_level_messages( 'rss', $user_level->id );
}
return $text; // Return the default RSS text (from Advanced Settings page)
}
add_filter( 'pmpro_rss_text_filter', 'pca_rss_text_filter_by_level', 10, 1 );
/**
* Define and return the text to use based on the supplied $type and Level ID value
*
* @param string $type Type of message (rss|not_logged_in|non_member)
* @param int $level_id Membership Level ID
*
* @return string
*/
function pca_get_level_messages( $type, $level_id ) {
/**
* Format: array(
* level 1 ID => array(
* 'rss' => 'User doesn't have access to RSS feed message',
* 'not_logged_in' => 'User isn't logged in message',
* 'non_member' => 'User isn't a current/active member message'
* ),
* level 2 ID => array(
* 'rss' => 'User doesn't have access to RSS feed message',
* 'not_logged_in' => 'User isn't logged in message',
* 'non_member' => 'User isn't a current/active member message'
* ),
* level 3 ID => array(
* 'rss' => 'User doesn't have access to RSS feed message',
* 'not_logged_in' => 'User isn't logged in message',
* 'non_member' => 'User isn't a current/active member message'
* ),
* )
*
* FIXME: Update this array to match your needs!
*/
$messages = array(
// For user w/level ID # 0 (This can only mean the user isn't a member, and/or aren't logged in...)
0 => array(
'rss' => "Default RSS message for guest users, or users who haven't logged in. Use substitution variables like on the Advanced Settings page",
'non_member' => "Not active member, or not logged in. Use substitution variables like on the Advanced Settings page",
),
// For membership Level with ID # 1
1 => array(
'rss' => "Level specific RSS message for when user is logged in, but doesn't have access. Use substitution variables like on the Advanced Settings page",
'non_member' => "Level specific Not A Member Of This Level message for when user is logged in, but doesn't have access. Use substitution variables like on the Advanced Settings page",
),
// For membership Level with ID # 4
4 => array(
'rss' => "Level specific RSS message for when user is logged in, but doesn't have access. Use substitution variables like on the Advanced Settings page",
'non_member' => "Level specific Not A Member Of This Level message for when user is logged in, but doesn't have access. Use substitution variables like on the Advanced Settings page",
),
// Etc...
);
return $messages[ $level_id ][ $type ];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment