0

I know a solution exists for Wordpress admin from this Stack Exchange question: Change username before login However, while this works for logging into Wordpress directly, I am trying to use it for WooCommmerce login. It does not seem to be running the wp_authenticate hook as it does not modify the username and does not run my plugin at all.

This is what I have:

function add_prefix_by_blogid(&$username) {
    $blog_id = get_current_blog_id();
    // write_log('This is blog_id: ' . $blog_id);
    
    $blog_prefixes = array(
        1 => 'abcstores',
        4 => 'westend',
        6 => 'eastend'
    );

    if (array_key_exists($blog_id, $blog_prefixes)) {
        $prefix = $blog_prefixes[$blog_id];
        $username = $prefix ? $prefix . '-' . $username : $username;
        // write_log('This is new_username: ' . $username);
    }

}

add_action('wp_authenticate', 'add_prefix_by_blogid');

I essentially want to add a prefix according to blog_id to the username before logging in. So a username "Vi" would have "westend-Vi" authenticated if she was logging into the westend store. What hook would I use for the same functionality but for customers via WooCommerce?

1
  • I've just reviewed the WC user login code and it will fire wp_authenticate during the login process. So either (a) you're not adding your own action early enough in the WordPress load, or (b) when you're testing this the if if (array_key_exists($blog_id, $blog_prefixes)) { is always false - try removing this if in your testing to see if it updates $username
    – Paul G.
    Aug 29 at 12:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.