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

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/

Related

How to redirect user to another page if the user is logged in - WordPress Elementor Page Builder

I have created a registration form using Elementor Page Builder. Now, I want to redirect the user to a different page if he/she is trying to access that registration page after logging in.
Is there any Elementor hook available for that? I know the WordPress function called is_user_logged_in().
function my_logged_in_redirect() {
if ( is_user_logged_in() && is_page( 12 ) )
{
wp_redirect( get_permalink( 32 ) );
die;
}
}
add_action( 'template_redirect', 'my_logged_in_redirect' );
You should get the ids of the page where the form is and the id of the page you want to redirect the user to.
Code goes in your child theme functions.php file
Reference: here
The 'Content Area Not Found' error might appear on Elementor designed sites when you use that snippet and try to edit page of ID 12 (in your example) in certain cases.
To avoid this, add the following code before the if-statement of your snippet:
if ( \Elementor\Plugin::$instance->preview->is_preview_mode() ) {
return;
}

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.

remove redirection in wordpress

I'm trying to removeredirection to wp-login after I've moved the wp installation in a subdirectory for security issues. How can I remove the redirection from myurl.wyz/wp-admin? I want to access the backend from myurl.xyz/wp-subdirectory/wp-admin.
If you want to change the location of your wp-admin folder, you will face a lot of issues to keep things working. I would not suggest so actualy move this folder somewhere else.
You are talking about security, so I think you don't want the backend to be found with wp-admin because every wordpress user knows this path.
So there is a plugin for changing the url of your backend login. This will make your site more save, because the login form cannot be found that easy: https://wordpress.org/plugins/change-wp-admin-login/
EDIT: If you want to have custom redirection, you can create a plugin and adjust the code for your needs:
<?php
/*
Plugin Name: Redirect Wordpress Admin pages
Description: Redirect if accessed
Version: 1.0
*/
add_action( 'template_redirect', function() {
if( ( is_admin() ) ) {
if (!is_user_logged_in() ) {
wp_redirect( site_url( '/wp-subdirectory/wp-admin' ) );
exit();
}
}
});
add_action('wp_logout','auto_redirect_after_logout');
function auto_redirect_after_logout(){
wp_redirect( home_url() );
exit();
}
Hope this helps.

Unlogged user cannot see store and must go to login and successfully logged in users must be redirected to store

I am working on a Wordpress site with Woocommerce. I am using this function inside functions.php to redirect everyone who visits my store to login since it is a private area. Once the person logs in, they should be redirected to the store again. I have the following code implemented, which works for me to get all those trying to navigate my store to log in, I need to modify this code so that once logged in, it redirects me to the store page in woocommerce, in my case the page of the store is called "tienda".
To clarify: Users who are not registered or logged in, should not be able to see the store or cart or anything from woocommerce, I already implemented this with a wp-members plugin, if they try to navigate these woocommerce urls, they must go to login. When you have already logged in, you should go to the store, in my case you should not redirect to the previous link, but must inevitably go to the page called "tienda".
On the other hand, I would like to know if my function is missing something or is well built. Thank you!
function loggedoutuser_redirect() {
if (
! is_user_logged_in()
&& (is_woocommerce() || is_cart() || is_checkout())
) {
header('Location: ' . wp_login_url());
exit;
}
}
add_action('template_redirect', 'loggedoutuser_redirect');
function woocommerce_login_redirect_custom( $redirect, $user ) {
$redirect = wc_get_page_permalink( 'shop' );
return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'woocommerce_login_redirect_custom', 10, 2 );
Paste this above snippet at the end of your active child theme functions.php file or else if you are good in plugin development then create a custom plugin.
Snippet Explanation:
Create a custom function to redirect after logged in
Fetch the shop URL using WooCommerce helper function wc_get_page_permalink( 'shop' ) and assign it to the variable $redirect
Then return the URL
Hook the custom function to the filter hook woocommerce_login_redirect
Note: You can check the user role inside our custom function then based on the role you can redirect it to the dashboard if he is administrator else to the shop page.

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