How to redirect all non login visitors to specific page in wordpress? - wordpress

I am building a website in which user must login to view to pages. I have create a login page. I want to redirect to traffic to that page if they are not logged in.

I do not quite understand the functionality you need,
But you can do this php hard redirect (write in funct):
add_action( 'the_post', 'is_user_not_logged_do_redirect',9999);
function is_user_not_logged_do_redirect() {
global $post;
$page_register_id = 5566; // set your register page ID;
$login_page_id = 5567; // set your login page ID;
$redirect_code = 302; // redirect type;
if(!is_user_logged_in() && $post && $post->ID != $page_register_id &&
$login_page_id != $post->ID){
wp_redirect(get_permalink($page_register_id),$redirect_code);
}
}
P.S. this redirect not better way for SEO and Google =)

Related

Wordpress redirect to login page if user is not logged in

I need to force users to login before they can see browse my site. For new users Access should only be allowed to the my-account page which is my login url i.e. example.org/my-account
Below is what i've done so far
add_action('wp','force_register' );
function force_register() {
$loggedin = is_user_logged_in();
$pagename = get_query_var('pagename'); //get current page name
if(strcmp("my-account",$pagename) != 0){ //if the user is looking at any other page other than my-account page
if(!$loggedin){ //if the user is not logged in
wp_safe_redirect( "/my-account", 302 ); //redirect to the login page
exit;
}
}
The browser can't redirect and complains about too many redirects
What am I missing ?
Before WordPress does any routing, processing, or handling. It is run in the main() WP method in which the $query_args are passed to parse_request(), as well as when send_headers() , query_posts(), handle_404(), and register_globals() are setup. This is caused for your issue, You may use init instead of wp hook. more about wp hook.

How to Redirect Registration page to My Account page if user is logged in?

I have a custom registration page and working fine unless I am logged-in. When I am login and click on my registration page it takes me to the registration page but it is blank. I want it to redirect the user if he/she logged-in to the my account page.
You can do it like this in your header.php
$post = get_post();
if(is_user_logged_in()) {
if($post->slug == 'regitrate-slug-here'){
header( 'Location: /my-page-slug-here' );
}
}
or just paste the link in the if statement
if(is_user_logged_in()) {
My page
} else {
Register
}
To Redirect custom Registration page to My Account page you should try small javascript something like this.
Write this code in your php file that you have created for registration.
if(is_user_logged_in()) {
echo("<script>location.href = '".home_url('my-account')."'</script>");
exit;
}

Hide wp-login.php as log-in and redirect it to a custom login page

I'm trying to hide the wp-login.php on my site so I installed the plugin Rename wp-login.php, I renamed it as log-in. Now I want to redirect it to my custom login page so the default login form of wordpress is totally hidden. Is there a way to accomplish this? I have already tried a redirection plugin and the code below however it only supports the wp-login.php and not my new login url:
function redirect_login_page(){
// Store for checking if this page equals wp-login.php
$page_viewed = basename( $_SERVER['REQUEST_URI'] );
// permalink to the custom login page
$login_page = get_permalink( '10' );
if( $page_viewed == "wp-login.php" ) {
wp_redirect( $login_page );
exit();
}
}
Is there a way to do this without actually altering the wordpress base files?
Try this
add_action('init','custom_login');
function custom_login(){
global $pagenow;
if( 'wp-login.php' == $pagenow ) {
wp_redirect('http://localhost/wordpresstest/blog/');
exit();
}
}
Try This Out
// Hook the appropriate WordPress action
add_action('init', 'prevent_wp_login');
function prevent_wp_login() {
// WP tracks the current page - global the variable to access it
global $pagenow;
// Check if a $_GET['action'] is set, and if so, load it into $action variable
$action = (isset($_GET['action'])) ? $_GET['action'] : '';
// Check if we're on the login page, and ensure the action is not 'logout'
if( $pagenow == 'wp-login.php' && ( ! $action || ( $action && ! in_array($action, array('logout', 'lostpassword', 'rp', 'resetpass'))))) {
// Load the home page url
$page = get_bloginfo('url');
// Redirect to the home page
wp_redirect($page);
// Stop execution to prevent the page loading for any reason
exit();
}
}
You can achieve the results of accessing wordpress admin via custom admin login page using a plugin named All In One WP Security & Firewall.
The All In One WordPress Security plugin will take your website security to a whole new level. This plugin is designed and written by experts and is easy to use and understand.It reduces security risk by checking for vulnerabilities, and by implementing and enforcing the latest recommended WordPress security practices and techniques.
Your requirement:
This plugin has ability to hide admin login page. Rename your WordPress login page URL so that bots and hackers cannot access your real WordPress login URL. This feature allows you to change the default login page (wp-login.php) to something you configure.
For more features, refer plugin codex
you can refer this plugin https://wordpress.org/plugins/custom-login-url/
or
https://www.inkthemes.com/how-to-redirecting-wordpress-default-login-into-a-custom-login-page/

change homepage for logged-in users in wordpress

In wordpress i want a different homepage for logged-in users and those who are logged-out (simple changing the redirect url when somebody clicks on the title of my website). Is there a way of doing this by adding a code snippet in my themes functions.php? I tried:
add_filter('get_the_permalink','my_permalink_redirect');
function my_permalink_redirect($permalink) {
if( is_user_logged_in() ) {
global $post;
if ($post->ID == 1) {
$permalink = 'http://localhost/homepage-for-logged-users';
}
}
return $permalink;
}
unfortunately it doesn't work and it would be unhandy to change the function from localhost to my domain name when i upload the site to a live hosting. can somebody give me some advice? Thanks!
edit:I got it working with the pluging "login with ajax" where I could define redirects after logins and I putted this snippet in my functions.php.
// Redirect users who arent logged in and on a 404 to the home page for logged out users
function login_redirect() {
// Check to see if user in not logged in and on the home page for logged users
if(!is_user_logged_in() && is_404()) {
// If user is, Redirect to home page for logged out users.
wp_redirect(home_url('home-logged-out'));
exit;
}
}
// add the block of code above to the WordPress template
add_action( 'wp', 'login_redirect' );
*I made the pages for logged-in users private so logged-out users will see 404. Not a very clean method, but it works for now...
Set your default home page to the page logged in users should see.
Add this code to functions.php:
add_action('init', 'redirect_user');
// for users not logged in
function redirect_user(){
if( !is_user_logged_in() ) {
wp_redirect( 'http://www.YourSite.com/SomePage');
exit;
}
}
Logged in users will see what they should see, and those not logged in will be redirected.

Woocommerce/Wordpress - Redirect User Login to Homepage

I have searched for the answer to this, used plugins and still nothing works.
I would like users of my site to be redirected to the home page after they login/register.
Currently, the user logs in and is redirected to the my account page.
Woocommerce provides this code, but it failed to work for me:
/*
* goes in theme functions.php or a custom plugin
*
* By default login goes to my account
**/
add_filter('woocommerce_login_widget_redirect', 'custom_login_redirect');
function custom_login_redirect( $redirect_to ) {
$redirect_to = 'http://anypage.com';
}
I have also attempted to use the Peter's Redirect plugin but it does not work since woocommerce bypasses wp-login.php.
Thoughts?
You can download http://wordpress.org/extend/plugins/peters-login-redirect/ and replace this in the widget-login.php
<input type="hidden" name="redirect_to" class="redirect_to" value="<?php echo $redirect_to; ?>
with
<input type="hidden" name="redirect_to" class="redirect_to" value="<?php echo site_url('/wp-content/plugins/peters-login-redirect/wplogin_redirect_control.php/'); ?>" />
That way you can use peters login redirect to redirect globally and specific users using the woocommerce login widget.
Make sure you change "Use external redirect file. Set this to "Yes" if you are using a plugin such as Gigya that bypasses the regular WordPress redirect process (and allows only one fixed redirect URL). Then, set the redirect URL to %s" in the settings to "YES".
Hope this helps.
Use this code on functions.php
add_filter('woocommerce_login_redirect', 'wc_login_redirect');
function wc_login_redirect( $redirect_to ) {
$redirect_to = 'https://www.google.co.in/';
return $redirect_to;
}
That fix they provide only works if you are utilizing the login widget. All you need to do it redirect the user after logging in to your home page is to add this to your login form:
<input type="hidden" name="redirect" value="<?php echo get_home_url(); ?>" />
There has been a filter recently added to allow you to redirect after registration. Doing a redirect on login is as simple as Tomanow mentions above (putting it in form-login.php). Here is a link to the filter and some instructions for handling this on the registration form.
https://github.com/woothemes/woocommerce/commit/014e31952828377bf7a1ebf4e812a43d0bcefa67#commitcomment-3351995
Use this code on functions.php
add_action('wp_logout','go_home');
function go_home(){
wp_redirect( home_url() );
exit();
}
Do this:
Go to admin > Woocommerce > Settings > General
Find "Customer Accounts" under "Cart, Checkout and Accounts"
Uncheck "Prevent customers from accessing WordPress admin"
Save Changes and test!
You can set redirect according to user role. Given bellow the code for that redirection and I have developed a WooCommerce Login or Register Redirect plugin for entry level user or nor technical or no-programmer.
//Redirect users to custom URL based on their role after login
function wp_woo_custom_redirect( $redirect, $user ) {
// Get the first of all the roles assigned to the user
$role = $user->roles[0];
$dashboard = admin_url();
$myaccount = get_permalink( wc_get_page_id( 'my-account' ) );
if( $role == 'administrator' ) {
//Redirect administrators to the dashboard
$admin_redirect = get_option('admin_redirect');
$redirect = $admin_redirect;
} elseif ( $role == 'shop-manager' ) {
//Redirect shop managers to the dashboard
$shop_manager_redirect = get_option('shop_manager_redirect');
$redirect = $shop_manager_redirect;
} elseif ( $role == 'customer' || $role == 'subscriber' ) {
//Redirect customers and subscribers to the "My Account" page
$customer_redirect = get_option('customer_redirect');
$redirect = $customer_redirect;
} else {
//Redirect any other role to the previous visited page or, if not available, to the home
$redirect = wp_get_referer() ? wp_get_referer() : home_url();
}
return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'wp_woo_custom_redirect', 10, 2 );
Also include this for standard WordPress login form logins that use wp-login.php:
add_filter('login_redirect', 'custom_login_redirect');
I use this filter and the one you have given, against the same filter function, and it catches the two places where a user may log in (WP and WC).
Also just noticed the obvious problem. You have this in your filter function:
$redirect_to = 'http://anypage.com';
but you need this:
return 'http://anypage.com';
$return_url is passed in from previous filters, so you can inspect it and decide whether to change it.
Edit:
Actually, I need to correct some of this. It looks like the woocommerce_login_widget_redirect filter is used to set the redirect parameter in the widget login form. This means it only fires when the user is not logged in and the widget login form is presented. It cannot be used to decide where to send the user after they have logged in.
The WP login_redirect filter fires after the user has logged in, and can modify any redirect_to URL sent with the login form to enable you to redirect the user to some other page.
In addition, the WooCommerce login widget does not handle the logins that you would assume it does. The login process is handled via AJAX in woocommerce-ajax.php You will see in there that the redirect URL is not passed through the woocommerce_login_widget_redirect filter and so cannot be modified. I'm going to raised this as a pull request with WooCommerce, because I believe it should be filtered.
https://github.com/woothemes/woocommerce/pull/2508

Resources