大家晚上好。我试着在网上寻找解决这个问题的方法,但没有找到任何相关的东西。我只需要编写一个代码片段,防止用户通过隐藏"my account"字段来更改其显示名称。Page或者其他更聪明的方法。谢谢你
从你的问题,我可以假设你正在使用WooCommerce。你需要做几件事来实现它:
- 删除account_display_name字段从我的帐户模板文件: 首先,复制form-edit-account.php文件
- Usewoocommerce_save_account_details_required_fields挂钩取消account_display_name字段。这是必要的,以避免在我的帐户页面保存更改时出现验证错误。
/wp-content/插件/woocommerce/模板/myaccount/form-edit-account.php
到子主题文件夹
/wp-content/主题/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' ); ?> <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>
将以下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;
}
谢谢你的问题。您可以将此代码插入到您的主题functions.php或使用代码片段插件。希望能成功。
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 );