Skip to content

Instantly share code, notes, and snippets.

@andreuka
Last active April 12, 2016 18:17
Show Gist options
  • Save andreuka/22058e684b63d0d9d83ade77d50f04a8 to your computer and use it in GitHub Desktop.
Save andreuka/22058e684b63d0d9d83ade77d50f04a8 to your computer and use it in GitHub Desktop.
Redirect Woocommerce login to refferer page
<?php
/*
* Author: Andrij Tkachenko https://www.upwork.com/fl/andrijtkachenko
*
*/
class Custom_Redirect{
public function __construct()
{
add_action('woocommerce_login_form_end', array($this, 'add_redirect_field'));
add_action('woocommerce_register_form_end', array($this, 'add_redirect_field'));
add_filter('woocommerce_login_redirect', array($this, 'login_redirect'), 10 ,2);
add_filter('woocommerce_registration_redirect', array($this, 'login_redirect'), 10 ,1);
}
public function add_redirect_field(){
$redirect = '';
if(!empty($_SERVER['HTTP_REFERER'])) $redirect = $_SERVER['HTTP_REFERER'];
echo '<input type="hidden" name="redirect" value="'.$redirect.'">';
if(is_checkout()){
echo '<input type="hidden" name="is_checkout" value="1">';
}
}
public function login_redirect($redirect, $user = false){
if(isset($_POST['is_checkout']) && ($_POST['is_checkout'] === '1')){
return get_permalink(wc_get_page_id( 'checkout' ));
}
if(isset($_POST['redirect']) && !empty($_POST['redirect'])){
return $_POST['redirect'];
} else {
return home_url('');
}
}
}
$run = new Custom_Redirect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment