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 petersplugins/5b304277ab1332188a6d9b9ff65ebfab to your computer and use it in GitHub Desktop.
Save petersplugins/5b304277ab1332188a6d9b9ff65ebfab to your computer and use it in GitHub Desktop.
<?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