-
-
Save Lonsdale201/5ab657dbcf104749a7f779734e6e8755 to your computer and use it in GitHub Desktop.
Basic Membership for WordPress
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
// place the code in the child theme functions.php or a custom code snippets plugin, like FluentSnippets | |
// Only define the $GLOBALS with your details only | |
// Default global settings | |
$GLOBALS['membership_protected_pages'] = [25, 2, 3]; // page IDs, specify the pages that can be accessed by just logging in | |
$GLOBALS['membership_redirect_url'] = '/login'; // Redirect URL if logged out user try to open a protected page | |
$GLOBALS['membership_hide_elements'] = ['.elementor-location-header', '.xyz', '#asd']; // Classes and IDs use comma separate, this will hide (display none), from those who have not logged in | |
$GLOBALS['membership_hide_blog'] = true; // Hide the entire blog, blog post, and archives from not logged in users set to false to disable this function | |
function membership_protect_content() { | |
global $membership_protected_pages, $membership_redirect_url, $membership_hide_blog; | |
if (!is_user_logged_in()) { | |
if (is_page($membership_protected_pages)) { | |
wp_redirect($membership_redirect_url ? $membership_redirect_url : home_url()); | |
exit; | |
} | |
if ($membership_hide_blog && (is_home() || is_single() || is_category() || is_tag())) { | |
wp_redirect($membership_redirect_url ? $membership_redirect_url : home_url()); | |
exit; | |
} | |
} | |
} | |
add_action('template_redirect', 'membership_protect_content'); | |
function membership_hide_elements() { | |
global $membership_hide_elements; | |
if (!is_user_logged_in() && !empty($membership_hide_elements)) { | |
echo '<style>'; | |
foreach ($membership_hide_elements as $selector) { | |
echo $selector . ' { display: none !important; }'; | |
} | |
echo '</style>'; | |
} | |
} | |
add_action('wp_head', 'membership_hide_elements'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment