I have a custom template that I built which manages a separate table inside a WordPress database. I have had success restricting access unless logged in on all other pages (including existing custom templates) accept my custom template (page id 9597) using this code:
add_action( 'template_redirect', 'add_restrict_access');
function add_restrict_access(){
if( ! is_user_logged_in() && is_page( 9597 ) ) {
wp_redirect( '/wp-login.php' );
exit;
}
}
If I change the is_page number to any other page ID, the redirect works. Anyone have any ideas?
SSL cert issue. Page was viewable without SSL, as soon as I added https it started working properly. Weird.
Related
I'm currently building my own website using wordpress.
I designed my own login - page and want users to be redirected to that page if they are not logged in.
There are several solutions to this problem, however, none of them seem to work.
I tried:
Adding code to the functions.php, such as:
<?php
function admin_redirect() {
if ( ! is_user_logged_in() ) {
wp_redirect( home_url( '/{custom page goes here}/' ) );
exit;
}
}
add_action( 'get_header', 'admin_redirect' );
It apparently didn't work - I also tried some other code like (to my header.php):
<?php
if ( ! is_user_logged_in() ) {
wp_redirect('http://somepagehere');
exit;
}
Use of plugins - I tried using "Force Login" and "WPS Hide Login".
First of all, installing "Force Login" didn't work when accessing the main page and only prohibited access when adding a slug to the main page (ex.: oho.nachhilfe.de/xyz). It also didn't redirect to the login page, but instead simply redirected to a 404 page (no matter what my login URL was).
Changing my login URL to a custom one using "WPS Hide Login" also didn't work, as this only replaced my custom login page with the default wordpress login page, therefore, my custom page simply got overwritten with the wordpress one.
Any ideas how to solve the issue?
Here's the page I need help with: https://www.oho-nachhilfe.de
First of all, let's all agree that the login customization handling in WordPress is awful.
Like Chris said, forward compatibility and messing with core don't go well together.
If it's just a permalink question you can always customize them instead of re-creating the whole login system.
Moving on. Both of your spinets are perfectly working, tho get_header is the wrong hook to use. Redirects needs to happen before determining which template to load. which is happening before get_header but after init and wp.
You can use, template_redirect do do just that.
Fires before determining which template to load.
<?php
add_action( 'template_redirect', function () {
if ( ! is_user_logged_in() && ! is_page( 'login' ) ) {
wp_safe_redirect( home_url( '/login/' ) );
exit;
};
} );
You can refer to the WP Plugin API/Action Reference to have a better understanding of the hook firing sequence.
I'm working on an existing wordpress project and i'm using ultimate member plugin. I think that to restrict access to pages if user is logged or not we have to pay for additional modules. So i tried php code snippets. I tried to use XYZ PHP code snippet but the snippet code is not working although it's active. I think i have to add the snippet created somewhere but don't know where plus the wordpress is in french. Can i go directly to a page edit and add snippet here is the code snippet :
<?php
if ( !is_user_logged_in() ) {
wp_redirect( 'https://xxxxir.com/register' );
//auth_redirect();
}
xyz-ips snippet="redirection"
Thanks.
If you want to restrict specific page you can do it this way.
Page that you want to restrict should have slug such as xxx.
// Put these codes in function.php
// xxx is slug of your page
add_action('wp_head', function(){
global $post;
$post_slug = $post->post_name;
if ( $post_slug == 'xxx' && !is_user_logged_in() ) {
wp_redirect( 'https://xxxxir.com/register' );
}
});
i have wordpress blog. I use different templates for blog posts and medical cases. I installed plugin for creating custom templates per post and it do their job. But now want medical cases to be available just for logged in users.
Page that i want to manage is using my custom template:
So i search in google for function that will limit access by template and by logged in status. And writen this function:
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
if ( is_page_template( $template = 'templates/clinic_case.php' ) && is_single() && ! is_user_logged_in() ) {
wp_redirect( 'url/wp-login.php', 301 );
exit;
}
}
but when try to load, content is loading just fine, and should not. Any help?
My guess is that by the time Wordpress processes your code, it has already passed the template_redirect hook.
I'm doing a shop online with Woocommerce, where my client ask me he don't want people to be able to register, he will create a user and password for each one of his clients. That way he can control who buys on his shop.
So I went into Woocommerce and disable the registration at checkout and everywhere, and the option to allow guests to place orders. Everything works fine, except that when someone tries to place an order, when logged out, when he tries to go to the checkout page, it just shows an unformatted message saying "You must be logged in to place an order". Is there a way where I can redirect not logged in customers to login page, when trying to access checkout?
May be this code could be more compact, simple and convenient:
add_action('template_redirect','check_if_logged_in');
function check_if_logged_in() {
if(!is_user_logged_in() && is_checkout())
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and fully functional.
Reference: WooCommerce login redirect based on cart
Use the following code in functions.php
add_action( 'template_redirect', 'redirect_user_to_login_page' );
function redirect_user_to_login_page(){
// Make sure your checkout page slug is correct
if( is_page('checkout') ) {
if( !is_user_logged_in() ) {
// Make sure your login page slug is correct in below line
wp_redirect('/my-account/');
}
}
}
I manage to do it my self! But still if someone has a cleaner code to do so, I'm open to suggestions.
What I did was to paste the following code on the functions.php file of the child theme:
add_action('template_redirect','check_if_logged_in');
function check_if_logged_in()
{
if(!is_user_logged_in() && is_checkout())
{
$url = add_query_arg(
'redirect_to',
get_permalink($pagid),
site_url('/my-account/')
);
wp_redirect($url);
exit;
}
}
Thanks
kind regards
I am trying to develope an password role protection on my web. What I need to achieve is to redirect visitors to the login wp-login.php when they try to enter an specific page if they are Suscriptors. WordPress don't gives us that option so I had to develop myself (I don't want to use external plugins).
I had the role filter, but I can't find the filter to control wich page is being loaded in order to redirect the visitor.
Add this code in your Function.php file.
using template_redirect action hook
This action hook executes just before WordPress determines which template page to load. It is a good hook to use if you need to do a redirect with full knowledge of the content that has been queried.
function template_redirect_fn()
{
//add you logic to perform your task
//Redirect page to login page
if(is_page ('about-us'))
{
$loginUrl = "LOGIN URL";
wp_redirect($loginUrl);
exit();
}
//if more then one page then used this
if( is_page( array( 'about-us', 'contact', 'management' ) )
// about us, or contact, or management page is in view
}
add_action( 'template_redirect', 'template_redirect_fn' );
is_page(Page ID OR title OR slug of you want to check )