1. 程式人生 > 實用技巧 >#wordpress如何隱藏掉last-name姓氏和first-name名字兩個欄位

#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' );