Change WordPress user roles and capabilities › Forums › Restrict or Permit access inside WordPress – how to › Restrict access + elementor template › Reply To: Restrict access + elementor template
17/12/2020 at 04:10
#7219
Vladimir
Keymaster
This script placed into functions.php file of the active theme of added as a Must Use plugin will redirect not logged-in user to the wp-login.php page:
// Block URLs from the list for not logged-in users
add_action('wp_head', 'your_prefix_protect_urls', 110);
function your_prefix_protect_urls() {
if ( is_user_logged_in() ) {
return;
}
$blocked_paths = array(
'/wp-test2/category/post-category-1/'
);
$redirect = false;
$path = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
foreach( $blocked_paths as $blocked_path ) {
if ( $blocked_path==$path ) {
$redirect = true;
break;
}
}
if ( !$redirect ) {
return;
}
// URL where do you wish redirect not logged-in user
$redirect_to = '/wp-test2/wp-login.php';
if (headers_sent()) {
?>
<script>
document.location.href = '<?php echo $redirect_to; ?>';
</script>
<?php
} else {
wp_redirect( $redirect_to );
}
die;
}
Just replace blocked path from ‘/wp-test2/category/post-category-1/’ to yours, like ‘/cat/tutoriels/’, and $redirect_to value to from ‘/wp-test2/wp-login.php’ to the path/URL of your login page.