Skip to content

Instantly share code, notes, and snippets.

@braddalton
Created July 10, 2017 09:48
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 braddalton/5d0be47605b3d609b80407c720c9855e to your computer and use it in GitHub Desktop.
Save braddalton/5d0be47605b3d609b80407c720c9855e to your computer and use it in GitHub Desktop.
Hide Posts From Logged Out Users https://wp.me/p1lTu0-gTu
<?php Do NOT copy this opening php tag
add_filter( 'the_content', 'filter_content', 10, 1 );
function filter_content( $content ) {
$limit = wp_trim_words( get_the_content(), 20, '.....' );
$login_form = sprintf( '<p><h4 class="register-link"><a href="%s">Register</a> or login below to view all content.</h4></p>', esc_url( wp_registration_url() ) );
$login_form .= sprintf( '<p class="login-form">%s</p>', wp_login_form( array( 'echo' => false ) ) );
$limit = $limit . $login_form;
$output = ! is_user_logged_in() && in_category( 'lifestyle' ) && is_singular( 'post' ) ? $limit : $content;
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment