You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何禁用WooCommerce客户账户详情页的邮箱字段?

Absolutely doable! Let’s break down how to implement this for your WordPress (likely WooCommerce, given the checkout mention) site:

Solution Overview

This requirement is totally achievable with custom code hooks in WordPress/WooCommerce. We’ll cover two key parts: hiding the email field from the account details page, and blocking any attempts to modify the email via form submissions (even if someone tries to bypass the UI).

1. For WooCommerce Sites (Most Common for Checkout Flows)

If you’re using WooCommerce for your checkout and user accounts, add these snippets to your theme’s functions.php file or a custom plugin (always back up your site before adding code):

Hide Email Field from My Account Edit Page

This removes the email field entirely from the frontend account edit form:

// Remove email field from WooCommerce My Account edit details
add_filter( 'woocommerce_edit_account_form_fields', 'remove_account_email_field' );
function remove_account_email_field( $fields ) {
    unset( $fields['account_email'] );
    return $fields;
}

Block Email Modification Attempts

Even if a user manually edits the form HTML to add the email field back, this snippet will block any submission that tries to change the email:

// Prevent email modification in WooCommerce account updates
add_action( 'woocommerce_save_account_details_errors', 'block_email_change_in_account' );
function block_email_change_in_account( $errors ) {
    $current_user = wp_get_current_user();
    if ( isset( $_POST['account_email'] ) && $_POST['account_email'] !== $current_user->user_email ) {
        $errors->add( 'email_restricted', __( 'Your email address cannot be modified after account creation.', 'woocommerce' ) );
    }
}

2. For Non-WooCommerce WordPress Sites

If you’re using a custom checkout flow without WooCommerce, use these snippets to restrict email changes:

Hide Email Field on Frontend Profile Page

This uses a small script to hide the email field from the logged-in user’s profile edit page:

// Hide email field on frontend user profile
add_action( 'show_user_profile', 'hide_frontend_profile_email' );
add_action( 'edit_user_profile', 'hide_frontend_profile_email' );
function hide_frontend_profile_email( $user ) {
    if ( $user->ID === get_current_user_id() ) {
        ?>
        <script>
            document.querySelector('tr.user-user-login-wrap + tr').style.display = 'none';
        </script>
        <?php
    }
}

Block Email Update Submissions

This stops any attempt to update the email via the profile form:

// Block email updates for frontend users
add_action( 'personal_options_update', 'block_frontend_email_update' );
add_action( 'edit_user_profile_update', 'block_frontend_email_update' );
function block_frontend_email_update( $user_id ) {
    if ( get_current_user_id() !== $user_id ) return; // Only apply to the logged-in user
    
    $current_email = get_userdata( $user_id )->user_email;
    if ( isset( $_POST['email'] ) && $_POST['email'] !== $current_email ) {
        wp_redirect( get_edit_profile_url( $user_id ) );
        exit;
    }
}

Quick Notes

  • Always test these changes on a staging site first to avoid breaking your live site.
  • Using a custom plugin for these snippets is better than functions.php because your changes won’t be lost when you update your theme.

内容的提问来源于stack exchange,提问作者Alex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:21:25