Skip to content

Instantly share code, notes, and snippets.

@atwellpub
Last active May 6, 2023 21:10
Show Gist options
  • Save atwellpub/a34257907d845da0535178462f9a3e5c to your computer and use it in GitHub Desktop.
Save atwellpub/a34257907d845da0535178462f9a3e5c to your computer and use it in GitHub Desktop.
Juptiter WordPress theme. Custom redirect after login.
<?php
/**
* Redirect admins to the dashboard and other users to //kmizs.com/user/
*/
add_filter( 'login_redirect', 'codeable_custom_login_redirect', 11, 3 );
function codeable_custom_login_redirect( $redirect_to, $request, $user ) {
if ( is_array( $user->roles ) ) {
if ( in_array( 'administrator', $user->roles ) ) {
return site_url( 'wp-admin' );
}
else {
return site_url('user');
}
}
return $redirect_to;
}
/**
* Accounts for WooCommerce Login redirect
*/
add_filter( 'woocommerce_login_redirect', 'codeable_custom_woocommerce_login_redirect' );
function codeable_custom_woocommerce_login_redirect( $redirect_to, $request, $user ) {
return site_url('user');
}
/*
* Overwrite default theme ajax redirect url
*/
add_action('wp_footer', 'codeable_switch_login_url' , 11 );
function codeable_switch_login_url() {
global $mk_options;
$theme_js_hook = ($mk_options['minify-js'] == 'true') ? 'theme-scripts-min' : 'theme-scripts';
wp_localize_script($theme_js_hook, 'ajax_login_object', array(
'ajaxurl' => admin_url('admin-ajax.php') ,
'redirecturl' =>site_url('user'),
'loadingmessage' => __('Sending user info, please wait...', 'mk_framework')
));
}
/**
* Redirects AJAX Registration's 'Lost password' and 'Create Account' buttons to woocommerce links.
*/
add_filter( 'wp_footer', 'codeable_redirect_home' );
function codeable_redirect_home( $registration_redirect ) {
/* gets permalink to registration page */
$permalink = get_permalink(6054); /* this can be changed to any URL */
?>
<script>
jQuery( document ).ready(function() {
jQuery('input[name="redirect_to"]').val('<?php echo $permalink; ?>');
jQuery('.mk-forget-password').attr('href', 'https://kmizs.com/my-account/lost-password/');
//jQuery('.mk-create-account').attr('href', 'https://kmizs.com/my-account/registro/');
jQuery('#mk_login_form #username').attr('required', 'required');
jQuery('#mk_login_form #password').attr('required', 'required');
//jQuery('.mk-forget-password,.mk-create-account').on('click' , function() {
// jQuery('.mk-login-register').hide();
//});
jQuery('.mk-forget-password').on('click' , function() {
jQuery('.mk-login-register').hide();
});
});
</script>
<?php
return site_url($permalink);
}
/**
* Redirects the user to the custom "Forgot your password?" page instead of
* wp-login.php?action=lostpassword.
*/
add_action( 'login_form_lostpassword', 'codeable_redirect_to_custom_lostpassword' );
function codeable_redirect_to_custom_lostpassword() {
if ( 'GET' == $_SERVER['REQUEST_METHOD'] ) {
if ( is_user_logged_in() ) {
wp_redirect(site_url('user'));
exit;
}
/* gets permalink to registration page */
$permalink = get_permalink(5942); /* http://kmizs.com/my-account/edit-account/ */
wp_redirect( $permalink );
exit;
}
}
/**
* Redirects the user to the custom "Set your password?" page instead of
* wp-login.php?action=rp.
*/
add_action( 'login_form_resetpass', 'codeable_redirect_to_custom_rp' );
function codeable_redirect_to_custom_rp() {
if ( 'GET' == $_SERVER['REQUEST_METHOD'] ) {
if ( is_user_logged_in() ) {
wp_redirect(site_url('user'));
exit;
}
/* gets permalink to registration page */
$permalink = get_permalink(6057); /* this can be changed to any URL - password/set/ */
$args = ($_REQUEST) ? $_REQUEST : array();
$permalink = add_query_arg( $args , $permalink);
wp_redirect( $permalink );
exit;
}
}
/**
* A shortcode for rendering the form used to initiate the password reset.
*
* @param array $attributes Shortcode attributes.
* @param string $content The text content for shortcode. Not used.
*
* @return string The shortcode output
*/
add_shortcode( 'custom-password-set-form', 'codeable_render_password_lost_form' );
function codeable_render_password_lost_form( $attributes, $content = null ) {
$default_attributes = array( 'show_title' => false );
$attributes = shortcode_atts( $default_attributes, $attributes );
if ( is_user_logged_in() ) {
return __( 'You are already signed in.', 'personalize-login' );
} else {
$html = '<div id="password-reset-form" class="widecolumn">
<form name="resetpassform" id="resetpassform" action="'.site_url( 'my-account/?action=register_setpass').'" method="post" autocomplete="off">
<input type="hidden" id="user_login" name="login" value="'.esc_attr( sanitize_text_field($_GET['login']) ).'" autocomplete="off" />
<input type="hidden" name="key" value="'.esc_attr( sanitize_text_field($_GET['key']) ).'" />';
if ( count( $attributes['errors'] ) > 0 ) {
foreach ( $attributes['errors'] as $error ) {
$html = ' <p>'.$error.'</p>';
}
}
$html .= ' <p>
<label for="pass1" class="pass-label">Nueva contraseña</label>
<input type="password" name="pass1" id="pass1" class="input" size="20" value="" autocomplete="off" />
</p>
<p>
<label for="pass2" class="pass-label" >Repita la nueva contraseña</label>
<input type="password" name="pass2" id="pass2" class="input" size="20" value="" autocomplete="off" />
</p>
<p class="description">'.wp_get_password_hint().'</p>
<p class="resetpass-submit">
<input type="submit" name="submit" id="resetpass-button"
class="button" value="Restablecer la contraseña" />
</p>
</form>
</div>';
return $html;
}
}
/**
* Resets the user's password if the password reset form was submitted.
*/
add_action( 'init', 'codeable_do_password_set' );
function codeable_do_password_set() {
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && isset($_REQUEST['action']) && $_REQUEST['action'] == 'register_setpass') {
$rp_key = sanitize_text_field($_REQUEST['key']);
$rp_login = sanitize_text_field($_REQUEST['login']);
$user = check_password_reset_key( $rp_key, $rp_login );
if ( ! $user || is_wp_error( $user ) ) {
if ( $user && $user->get_error_code() === 'expired_key' ) {
wp_redirect( home_url( 'my-account?login=expiredkey' ) );
} else {
wp_redirect( home_url( 'my-account?login=invalidkey' ) );
}
exit;
}
if ( isset( $_POST['pass1'] ) ) {
if ( $_POST['pass1'] != $_POST['pass2'] ) {
// Passwords don't match
$redirect_url = home_url( 'member-password-reset' );
$redirect_url = add_query_arg( 'key', $rp_key, $redirect_url );
$redirect_url = add_query_arg( 'login', $rp_login, $redirect_url );
$redirect_url = add_query_arg( 'error', 'password_reset_mismatch', $redirect_url );
wp_redirect( $redirect_url );
exit;
}
if ( empty( $_POST['pass1'] ) ) {
// Password is empty
$redirect_url = home_url( 'member-password-reset' );
$redirect_url = add_query_arg( 'key', $rp_key, $redirect_url );
$redirect_url = add_query_arg( 'login', $rp_login, $redirect_url );
$redirect_url = add_query_arg( 'error', 'password_reset_empty', $redirect_url );
wp_redirect( $redirect_url );
exit;
}
// Parameter checks OK, reset password
reset_password( $user, $_POST['pass1'] );
wp_redirect( home_url( 'my-account?password=changed' ) );
} else {
echo "Invalid request.";
}
exit;
}
}
/**
* redirect on login failed
*/
add_action( 'wp_login_failed', 'codeable_login_fail_redirect' ); // hook failed login
function codeable_login_fail_redirect( $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_safe_redirect('https://kmizs.com/my-account/?login=failed');
exit;
}
}
/**
* redirect on logout
*/
add_action( 'wp_logout', 'codeable_logout_redirect' ); // hook failed login
function codeable_logout_redirect( $username ) {
wp_safe_redirect('https://kmizs.com/my-account/lost-password/');
exit;
}
/**
* Lost password redirect
*/
add_filter( 'lostpassword_redirect', 'my_redirect_home' );
function my_redirect_home( $lostpassword_redirect ) {
return 'https://kmizs.com/my-account/lost-password/';
}
/**
* Redirect on registration failure
*/
add_action('register_post', 'codeable_register_fail_redirect', 99, 3);
function codeable_register_fail_redirect( $sanitized_user_login, $user_email, $errors ){
//this line is copied from register_new_user function of wp-login.php
$errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
//this if check is copied from register_new_user function of wp-login.php
if ( $errors->get_error_code() ){
//setup your custom URL for redirection
$redirect_url = get_bloginfo('url') . '/registrace';
//add error codes to custom redirection URL one by one
foreach ( $errors->errors as $e => $m ){
$redirect_url = add_query_arg( $e, '1', $redirect_url );
}
//add finally, redirect to your custom page with all errors in attributes
wp_redirect( $redirect_url );
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment