微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

#wordpress如何隐藏掉last-name姓氏和first-name名字两个字段

主要参考:Link

在对应的主题文件夹下的functions.PHP文件中,例如:wp-content\themes\vt-blogging\functions.PHP

参考原文说明

<?PHP

// Function to disable the first name and last name fields
function disable_first_and_last_name_fields() {
	?>
	<script type="text/javascript">
        $(function() {
            // disable the first and last names in the admin profile so that user's cannot edit these
				$('#first_name').prop( 'disabled', true );
				$('#last_name').prop( 'disabled', true );
        });
   	</script>
	<?PHP
}

// Action hook to inject the generated JavaScript into admin pages
add_action( 'admin_head', 'disable_first_and_last_name_fields' );

wordpress自带jQuery的,这里,作者通过wordpress的action钩子注入了一段jQuery脚本,该脚本的作用就是,通过css把相关的dom节点的disable属性设定为true。

最后的实现

注意,如果直接这么把代码贴过去,是不会生效的,需要把$换成jQuery关键字。详细的原因,见这里.

我更希望直接把这个dom通过css移除掉,所以最后我的解决方式是:

/* 隐藏掉姓氏和名字两个字段 */
// Function to disable the first name and last name fields
<?PHP
function disable_first_and_last_name_fields() {
	?>
	<script type="text/javascript">
		let $ = jQuery;
        $(function() {
            // disable the first and last names in the admin profile so that user's cannot edit these
			$('.user-first-name-wrap').css( 'display', 'none' );
			$('.user-last-name-wrap').css( 'display', 'none' );
        });
   	</script>
	<?PHP
}

// Action hook to inject the generated JavaScript into admin pages
add_action( 'admin_head', 'disable_first_and_last_name_fields' );

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐