主要参考:Link
在对应的主题文件夹下的functions.php文件中,例如:wp-content\themes\vt-blogging\functions.php中
参考原文说明
// Function to disable the first name and last name fields
function disable_first_and_last_name_fields() {
?>
$(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 );
});
}
// 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
function disable_first_and_last_name_fields() {
?>
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‘ );
});
}
// Action hook to inject the generated JavaScript into admin pages
add_action( ‘admin_head‘, ‘disable_first_and_last_name_fields‘ );
如果需要自定义脚本然后从functions.php中去引入,参见这里
原文:https://www.cnblogs.com/jaycethanks/p/13235061.html