wordpress WP嗅探器隐藏和防止用户更改account_display_name

tjjdgumg  于 5个月前  发布在  WordPress
关注(0)|答案(3)|浏览(70)

大家晚上好。我试着在网上找一个解决方案,但找不到任何相关的东西。我只需要写一个片段,通过隐藏“我的帐户”页面中的字段或任何其他更聪明的方法来防止用户更改其显示名称。谢谢

pu82cl6c

pu82cl6c1#

从你的问题,我可以假设你正在使用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_保存_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;
    }

mefy6pfw

mefy6pfw2#

感谢您的问题。您可以将此代码插入到您的主题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 );

字符串

li9yvcax

li9yvcax3#

最好的解决方案是在后端取消设置变量,并在前端禁用字段(就像Monzur在他的回答中所说的那样),这样用户就不会感到困惑。下面的代码(作为插件)为我做了这项工作:

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']);
    }
}

// From Manzurs answer
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');
            });
        </script>
    <?php
    }
}

字符串

相关问题