WP 代码片段隐藏并防止用户更改 account_display_name

问题描述 投票:0回答:3

大家晚上好。我试图在网上寻找解决方案,但找不到任何相关的内容。我只需要编写一个片段,防止用户通过隐藏“我的帐户”页面中的字段或任何其他可能的更智能的方式来更改其显示名称。谢谢你

php wordpress code-snippets
3个回答
4
投票

从您的问题我可以假设您正在使用 WooCommerce。您需要完成几件事才能实现它:

  1. 我的帐户模板文件中删除account_display_name字段:

首先,复制 form-edit-account.php 文件

/wp-content/plugins/woocommerce/templates/myaccount/form-edit-account.php

到您的子主题文件夹

/wp-content/themes/your_child_theme/woocommerce/myaccount/form-edit-account.php

然后打开复制的文件并删除与 account_display_name 字段相关的 html 标记和 php 代码:

    <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
    <label for="account_display_name"><?php esc_html_e( 'Display name', 'woocommerce' ); ?>&nbsp;<span class="required">*</span></label>
    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="account_display_name" id="account_display_name" value="<?php echo esc_attr( $user->display_name ); ?>" />
    <span><em><?php esc_html_e( 'This will be how your name will be displayed in the account section and in reviews', 'woocommerce' ); ?></em></span>
    </p>
    <div class="clear"></div>
  1. 使用 woocommerce_save_account_details_required_fields 挂钩从必填字段中取消设置 account_display_name 字段。这是为了避免在“我的帐户”页面上保存更改时出现验证错误所必需的。

将以下 php 代码放入您孩子的主题functions.php 文件中:

    add_filter('woocommerce_save_account_details_required_fields', 'remove_required_fields'); 
    function remove_required_fields( $required_fields ) {
        unset($required_fields['account_display_name']);
        return $required_fields;
    }

0
投票

谢谢你的提问。您可以将此代码插入主题functions.php或使用Code Snippets插件。希望它会起作用。

function wp_disable_display_name() {
    global $pagenow;
    if ( $pagenow == 'profile.php' ) {
    ?>
        <script>
            jQuery( document ).ready(function() {
                jQuery('#display_name').prop('disabled', 'disabled');
            });
        </script>
    <?php
    }
}
add_action( 'admin_head', 'wp_disable_display_name', 15 );

0
投票

最好的解决方案是在后端取消设置该变量,并在前端禁用该字段,这样用户就不会感到困惑。以下(作为插件)适合我:

add_action('personal_options_update', 'disable_name_edit');
add_action('edit_user_profile_update', 'disable_name_edit');

function disable_name_edit($user_id) {
    if (!current_user_can('edit_user', $user_id)) {
        return false;
    }

    // Prevent users from changing their first and last names
    if(isset($_POST['first_name'])){
        unset($_POST['first_name']);
    }
    if(isset($_POST['last_name'])){
        unset($_POST['last_name']);
    }
    if (isset($_POST['display_name'])) {
        unset($_POST['display_name']);
    }
    if (isset($_POST['nickname'])) {
        unset($_POST['nickname']);
    }
}

add_action( 'admin_head', 'wp_disable_display_name', 15 );
function wp_disable_display_name() {
    global $pagenow;
    if ( $pagenow == 'profile.php' ) {
    ?>
        <script>
            jQuery( document ).ready(function() {
                jQuery('#display_name').prop('disabled', 'disabled');
                jQuery('#first_name').prop('disabled', 'disabled');
                jQuery('#last_name').prop('disabled', 'disabled');
                jQuery('#nickname').prop('disabled', 'disabled');
            });
        </script>
    <?php
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.