Created
December 7, 2016 09:03
-
-
Save petersplugins/5b304277ab1332188a6d9b9ff65ebfab to your computer and use it in GitHub Desktop.
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 | |
// This code snippet hides exclusive content if the user is not logged in | |
// To hide content you have to create a category with the slug 'only-for-members' | |
// All posts assigned to this category will be hidden from not logged in users | |
// Add a custom function to filter the content | |
add_filter( 'the_content', 'exclusive_content', 99 ); | |
// If the user is not logged in, the excerpt will be displayed plus a message that the content is avialable only for registered users | |
// Also a link to the login page is displayed | |
// A link to the registration page is displayed only when registration of new users is allowed | |
function exclusive_content( $content ) { | |
if ( in_category( 'only-for-members' ) && ! is_user_logged_in() ) { | |
$content = '<p>' . get_the_excerpt() . '<br /><br />This content is available only for registered users. <a href="' . wp_login_url( get_permalink() ) . '" title="Login">Login</a>' . ( ( get_option( 'users_can_register' ) ) ? ' <a href="' . wp_registration_url() . '" title="Register">Register Now</a>' : '' ); | |
} | |
return $content; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment