So I'm making a WP-site with some posts that should be only visible, I want to make this automatic (so user doesn't have to set to private each time for each post) and thought that a function would be neatest. So I want to redirect all visitors that is not logged in if they visit a post with the category Intranet .
I added this to functions.php, but nothing happened when visiting a post with this category:
add_action('intra_post', 'intranet_post_privacy');
function intranet_post_privacy() {
if ( !is_user_logged_in() && is_category( 'intranet' )) {
header("Location: http://utt.se/user-login.php");
}
}
Add below code to theme's functions.php
change the $category_slug
variable according to your category.
add_action( 'template_redirect', 'redirect_to_login_for_specific_category' );
function redirect_to_login_for_specific_category() {
$category_slug = 'intranet';
global $post;
if ( ! is_user_logged_in() && in_category( $category_slug, $post ) ) {
$new_url = site_url() . "/wp-login.php";
wp_redirect( $new_url, 301 );
exit;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.