首页 > 其他 > 详细

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

时间:2020-07-04 15:48:18      阅读:78      评论:0      收藏:0      [点我收藏+]

主要参考: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‘ );

技术分享图片

如果需要自定义脚本然后从functions.php中去引入,参见这里

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

原文:https://www.cnblogs.com/jaycethanks/p/13235061.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!