Skip to content

Instantly share code, notes, and snippets.

@smeric
Last active August 29, 2015 14:09
Show Gist options
  • Save smeric/0c425adf9e9439e3c076 to your computer and use it in GitHub Desktop.
Save smeric/0c425adf9e9439e3c076 to your computer and use it in GitHub Desktop.
Redirect WooCommerce customer to profile page after a successful login through wp-login.php
<?php
/**
* Redirect WooCommerce customer to profile page after a successful login through wp-login.php or
* redirect any unauthorised user trying to access any wp-admin files.
*
* @author Sébastien Méric <[email protected]>
* @param string $redirect_to URL to redirect to.
* @return string
*/
add_action( 'init', 'woocommerce_custom_redirect_customer_to_account_page' );
function woocommerce_custom_redirect_customer_to_account_page() {
if ( is_admin() && ! current_user_can( 'edit_posts' ) &&
! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
$redirect_to = home_url();
$myaccount_page_id = get_option( 'woocommerce_myaccount_page_id' );
if ( $myaccount_page_id ) {
$redirect_to = get_permalink( $myaccount_page_id );
}
wp_redirect( $redirect_to );
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment