Skip to content

Instantly share code, notes, and snippets.

@TimBHowe
Last active April 14, 2020 14:17
Show Gist options
  • Save TimBHowe/45af8de920516b3ec7f6805fe0d54f9a to your computer and use it in GitHub Desktop.
Save TimBHowe/45af8de920516b3ec7f6805fe0d54f9a to your computer and use it in GitHub Desktop.
A custom function to have all users login using the WooCommerce My Account page if set
<?php
/**
* Update the Login URL to the WooCommerce My Account Page.
*
* @param string $login_url The login URL. Not HTML-encoded.
* @param string $redirect The path to redirect to on login, if supplied.
* @param bool $force_reauth Whether to force reauthorization, even if a cookie is present.
*
* @return string The login URL. Not HTML-encoded.
*/
function mytheme_use_myaccount_login( $login_url, $redirect, $force_reauth ) {
if ( function_exists( 'is_woocommerce' ) ) {
$woo_myaccount = wc_get_page_permalink( 'myaccount' );
if ( strpos( $login_url, 'wp-login.php' ) && ! empty( $woo_myaccount ) ) {
$params = array();
// Check for a redirect parameter.
if ( $redirect ) {
$params['redirect_to'] = $redirect;
}
// Check for the reauthorization parameter.
if ( $force_reauth ) {
$params['reauth'] = 1;
}
// Add parameters if needed and update the login URL to the WooCommerce My Accounts Page.
if ( ! empty( $params ) ) {
$params = urlencode_deep( $params );
$params = build_query( $params );
$login_url = $woo_myaccount . $params;
} else {
$login_url = $woo_myaccount;
}
}
}
return $login_url;
}
add_filter( 'login_url', 'mytheme_use_myaccount_login', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment