【发布时间】:2021-12-04 12:11:09
【问题描述】:
大家晚上好。我试图在网上寻找解决方案,但找不到任何相关的东西。我只需要编写一个 sn-p 来阻止用户通过隐藏“我的帐户”页面中的字段或任何其他更智能的方式来更改他们的显示名称。谢谢
【问题讨论】:
标签: php wordpress code-snippets
大家晚上好。我试图在网上寻找解决方案,但找不到任何相关的东西。我只需要编写一个 sn-p 来阻止用户通过隐藏“我的帐户”页面中的字段或任何其他更智能的方式来更改他们的显示名称。谢谢
【问题讨论】:
标签: php wordpress code-snippets
根据您的问题,我可以假设您正在使用 WooCommerce。你需要做几件事来实现它:
首先,复制 my-account.php 文件
/wp-content/plugins/woocommerce/templates/myaccount/my-account.php
到您的子主题文件夹
/wp-content/themes/your_child_theme/woocommerce/myaccount/my-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;
}
【讨论】:
/woocommerce/myaccount/ 文件夹中。更多信息请参考官方Woo template structure docs
感谢您的提问。您可以将此代码插入主题 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 );
【讨论】: