We want the users of a website to close the session without having to confirm it.
Currently, when we press the "Log Out" button, the user is directed to the URL htt://myWebSite.com/my-account/customer-logout/ and we must press "Confirm and Exit"
Well, we want to avoid this step and automatically log out when the user clicks the Logout Button
There is some plugin that could do this, but we prefer to use functions. We've tried things, but we can't get it right to get it done
add_action('wp_logout','ps_redirect_after_logout');
function ps_redirect_after_logout(){
wp_redirect( 'https://miPaginaFavorita.com' );
exit();
}
Another option that we have handled is the following, but it does not work either
add_action('check_admin_referer', 'logout_without_confirm', 10, 2);
function logout_without_confirm($action, $result)
{
/**
* Allow log out without confirmation
*/
if ($action == "log-out" && !isset($_GET['_wpnonce'])) {
$redirect_to = isset($_REQUEST['redirect_to']) ?
function logout_without_confirm($action, $result)
{
/**
* Allow log out without confirmation
*/
if ($action == "log-out" && !isset($_GET['_wpnonce'])) {
$redirect_to = isset($_REQUEST['redirect_to']) ?
$_REQUEST['redirect_to'] : '';
$location = str_replace('&', '&', wp_logout_url($redirect_to));;
header("Location: $location");
die();
}}
How can I modify my function to avoid confirming when logging out?
Thanks
you most change url function in your button . check your theme file how create button for logout .
use 'wp_logout_url' function for skip Confirm page .
in my theme like this :
if ( class_exists( 'WooCommerce' ) ) {
$logout_link = wc_get_endpoint_url( 'customer-logout', '', wc_get_page_permalink( 'myaccount' ) );
}
$logout_btn = '<a class="login" href="' . esc_url( $logout_link ) . '">Logout</a>';
For example, I did this to skip the page confirmation and work fine :
$logout_link = wp_logout_url( get_home_url() );
$logout_btn = '<a class="login" href="' . esc_url( $logout_link ) . '">Logout</a>';
I hope it helped you.
Edit --
METHOD 2 :
simple way => if you are redirect to "/customer-logout" when click logout button you can check and logout user with this function .
add code in your functions.php (is in your theme folder or child theme folder) .
function skip_logout_confirmation() {
global $wp;
if ( isset( $wp->query_vars['customer-logout'] ) ) {
wp_redirect( str_replace( '&', '&', wp_logout_url( home_url() ) ) );
exit;
}
}
add_action( 'template_redirect', 'skip_logout_confirmation' );
Related
On my Woocommerce shop settings I have checked the option:
[✓] Redirect to the cart page after successful addition.
And that's good behaviour for 99% of my shop.
On 1 single page (custom page with custom template) I need to enable Ajax functionality though.
Is there a way to accomplish this task in functions.php?
Ok insert this code into your functions.php. You have to replace the variable $your_ajax_page_slug with the name of your page that you want the redirect to cart functionality to be disabled. Ensure that you have 'Enable AJAX add to cart buttons on archives" checked in settings.
add_filter( 'woocommerce_get_script_data', 'modify_woocommerce_get_script_data', 20, 2 );
function modify_woocommerce_get_script_data ( $params, $handle ) {
global $wp;
$page_slug = '';
$your_ajax_page_slug = 'your-page-slug';
$current_url = home_url( $wp->request );
// Break the URL by the delimiter
$url_pieces = explode('/', $current_url);
// Get the page slug
if( is_array( $url_pieces ) )
$page_slug = end( $url_pieces );
if( $handle == 'wc-add-to-cart' && $page_slug == $your_ajax_page_slug ) {
$params['cart_redirect_after_add'] = false;
}
return $params;
}
I am using a custom login page in Wordpress, and I installed a script in the functions.php that redirect to a custom "error login" page (adding parameters to the login URL), so it can display the errors in the same page instead of the native login page.
But in some cases, this login page contains some parameters already, when someone is redirected there after trying to access a private page.
For example :
Regular case : They try to access : website.com/login. If there is an error when trying to login, my script redirect to website.com/login?login=empty, and they can try again.
Problematic case : They try to access website.com/protected-page/private, they are automatically redirected to website.com/login?members=%2Fprotected-page%2Fprivate%2. If there is an error when trying to login, my script actualy redirect to website.com/login?login=empty (same as regular case), and what I'm trying to achieve, is to redirect to website.com/login?members=%2Fprotected-page%2Fprivate%2&login=empty, when an error is made.
Keeping this "?members" parameter in the URL allow the form to redirect to the previous page they were trying to access before, after a successful login. Otherwise with the regular case, they are redirected to the general dashboard.
The script that actually manage the redirect when error is this one :
add_action( 'authenticate', 'check_username_password', 1, 3);
function check_username_password( $login, $username, $password ) {
$referrer = $_SERVER['HTTP_REFERER'];
if( !empty( $referrer ) && !strstr( $referrer,'wp-login' ) && !strstr( $referrer,'wp-admin' ) ) {
if( $username == "" || $password == "" ){
wp_redirect( get_permalink( 20 ) . "?login=empty" );
exit;
}
}
So the important part here, that will create the error URL is :
wp_redirect( get_permalink( ID ) . "?login=empty" );
I can get my other UTL parameter with "$_GET["members"]", if I do an "echo $_GET["wlfrom"];" it displays successfully the parameter in the page. But even if it looks so simple, I don't manage to add this parameter to the URL. What I'v tried so far :
#1. wp_redirect( get_permalink( ID ) . "?members=" . $_GET["members"] . "?login=empty" );
#2. wp_redirect( get_permalink( ID ) . "?members=" . echo $_GET["members"] . "?login=empty" );
#3. $param = array('members');
wp_redirect( get_permalink( ID ) . "?members=" . $param . "?login=empty" );
//This one returns website.com/?members=array/?login=empty
#4.$param = $_SERVER["PHP_SELF"];
wp_redirect( $param . "?login=empty" );
// This one I cannot use because it displays the native login URL, not my permalink, that's why I need to use "get_permalink" for the first part of the URL
And various other solutions too. I'm a newbie, so I can't find of other solutions to create this new URL based on the previous one, I'm not sure either if using GET is the way to go also.
What should I fill up "wp_redirect" with in order to use the parameter from the current URL ?
Thank you !
put a hidden field in your wordpress custom login form
<input type="hidden" name="members" value="<?php echo #$_REQUEST['members']; ?>">
and change these
wp_redirect( get_permalink( ID ) . "?login=empty" );
with
<?php
$queryarg='?login=empty';
if(isset($_REQUEST['members']) && $_REQUEST['members']!=''){
$queryarg .='&members='$_REQUEST['members'];
}
wp_redirect( get_permalink( ID ) . $queryarg );
?>
I am printing a custom login form with my custom theme, but when I submit the user credentials, I always get an error - even if they're correct.
Here is my functions.php:
function rockport_login_fail( $username ) {
$referrer = $_SERVER['HTTP_REFERER']; // where did the post submission come from?
// if there's a valid referrer, and it's not the default log-in screen
if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') ) {
wp_redirect( $referrer . '?login=failed' ); // let's append some information (login=failed) to the URL for the theme to use
exit;
}
}
function rockport_blank_login() {
$referrer = $_SERVER['HTTP_REFERER'];
if ( !strstr($referrer, 'wp-login') ) { // login1 is the name of the loginpage.
if ( !strstr($referrer, '?login=failed') ) { // make sure we don’t append twice
wp_redirect( $referrer . '?login=failed' ); // let’s append some information (login=failed) to the URL for the theme to use
} else {
wp_redirect( $referrer );
}
exit;
}
}
add_action( 'authenticate', 'rockport_blank_login');
add_action( 'wp_login_failed', 'rockport_login_fail' ); // hook failed login
What am I doing wrong? Thanks!
you forget to use to check $username.
you can also check the complete guide here
add_action( 'wp_login_failed', 'rockport_login_fail' ); // hook failed login
function rockport_login_fail( $username ) {
$referrer = $_SERVER['HTTP_REFERER'];
// if there’s a valid referrer, and it’s not the default log-in screen
if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') && $username!=null ) {
if ( !strstr($referrer, '?login=failed' )) { // make sure we don’t append twice
wp_redirect( $referrer . '?login=failed'); // let’s append some information (login=failed) to the URL for the theme to use
} else {
wp_redirect( $referrer );
}
exit;
}
}
add_action( 'authenticate', 'rockport_blank_login');
function rockport_blank_login( $username ){
$referrer = $_SERVER['HTTP_REFERER'];
if ( !strstr($referrer,'wp-login') && $username==null ) { // login1 is the name of the loginpage.
if ( !strstr($referrer, '?login=failed') ) { // make sure we don’t append twice
wp_redirect( $referrer . '?login=failed' ); // let’s append some information (login=failed) to the URL for the theme to use
} else {
wp_redirect( $referrer );
}
exit;
}
}
I am not very advanced in php.
I found function that i can use in WP header - then when login from "small green box" in header user stay on page from that he try to login.
Here is my page with WP: http://www.computers-and-control.com/service/manuals/
Now i have other issue, i copied file /wp-login.php to /wp-logincc.php this new file is used for login in my template WP-Download Manager - when click file - that need login before download than is used /wp-logincc.php for function login.
Problem is that after login from wp-logincc.php i am redirected to "Dashboard/Admin Panel" - i would go back to the subpage i come from.
I use different subpages for downloading files with required login and allways need come back to this one i come from.
I try in my wp-logincc.php code:
<?php
/**
* WordPress User Page
*
* Handles authentication, registering, resetting passwords, forgot password,
* and other user handling.
*
* #package WordPress
*/
/** Make sure that the WordPress bootstrap has run before continuing. */
require( dirname(__FILE__) . '/wp-load.php' );
// dankam tu cos ewentualnie zmienic ---- Redirect to https login if forced to use SSL
$dankam_aaa = apply_filters( 'fromwhereyoucame', $fromwhereyoucame );
function fromwhereyoucame( $link ) {
$currenturl = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
if ( !is_user_logged_in() )
$link = str_replace( '">', '?redirect_to=' . urlencode( $currenturl ) . '">', $link );
else
$link = str_replace( '">', '&redirect_to=' . urlencode( $currenturl ) . '">', $link );
return $link;
}
if ( force_ssl_admin() && ! is_ssl() && $dankam_aaa ) {
if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {
wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
exit();
} else {
wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
exit();
}
}
But this do nothing in /wp-logincc.php
(But this enclosed function "function fromwhereyoucame" works in "green box")
Please help to modify wp-logincc.php
why you not using plugin for this https://wordpress.org/plugins/theme-my-login/
there is a setting to set after login same page or dashboard redirect..
or
To achieve this redirect after login, add the following code to the functions.php file for your theme:
> if ( (isset($_GET['action']) && $_GET['action'] != 'logout') ||
> (isset($_POST['login_location']) && !empty($_POST['login_location']))
> ) {
> add_filter('login_redirect', 'my_login_redirect', 10, 3);
> function my_login_redirect() {
> $location = $_SERVER['HTTP_REFERER'];
> wp_safe_redirect($location);
> exit();
> } }
another solution to make changes by editing in core
wp-login.php
Line 424 - 426...
} else { $redirect_to = admin_url(); }
Change to...
} else { $redirect_to = $_SERVER[HTTP_REFERER]; }
I'm trying to pass the redirect_to querystring to the registration page so individuals who don't have a login, need to register, get redirected back to login page's referrer. I used
wp_login_url(get_permalink($event->get_id()))
to add the redirect_to querystring to the login page, and tried to use get get_query_var( 'redirect_to' ), but nothing happens.
Anyone know how to add the login querystring on redirect to the register page?
Figure it out, the applied filter in login.php allows tweaking of the url. I put the code below. The only thing missing is a message stating "You've successfully registered, your password should arrive in the mail".
function custom_register_url( $registration_url ) {
$registration_url = sprintf( '%s', esc_url( wp_registration_url() ), $_SERVER["QUERY_STRING"], __( 'Register' ) );
return $registration_url;
}
add_filter( 'register', 'custom_register_url' );
If you're looking to maintain the redirect_to url from the login to the registration and back to the login page, so the user can just enter their password once received by email, and still return to the original referrer this is the full solution.
function custom_register_url( $registration_url ) {
$redirect_to = $_GET["redirect_to"];
$eab_msg = $_GET["eab"];
if( $redirect_to != "" && $eab_msg != "" ) {
// change query name values to prevent default behaviour (redirect_to to uct_redirect)
$registration_url = sprintf( '%s', esc_url( wp_registration_url() ), "uct_redirect=" . urlencode($redirect_to), "uct_eab=" . urlencode($eab_msg), __( 'Register' ) );
}
return $registration_url;
}
add_filter( 'register', 'custom_register_url' );
function custom_registration_redirect($registration_redirect) {
$redirect_to = $_GET["uct_redirect"];
$eab_msg = $_GET["uct_eab"];
if( $redirect_to != "" && $eab_msg != "" ) {
// change query names back to original values (uct_redirect to redirect_to)
$registration_redirect = wp_login_url( $redirect_to ) . '&eab=' . $eab_msg;
}
return $registration_redirect;
}
add_filter( 'registration_redirect', 'custom_registration_redirect' );
Just drop it in functions.php. Hope this helps someone Cheers!