簡體   English   中英

如何使WordPress用戶配置文件中的自定義字段只讀?

[英]how to make custom field in WordPress user profile read only?

在 WordPress 中,我想將用戶配置文件中的自定義字段調整為只讀字段。

我已嘗試使用 Advanced Access Manager 插件實現此目的,但無法隱藏該字段。

在此處輸入圖片說明

據我所知,Edit User 和 Profile 頁面中只有三個過濾器鈎子:user_contactmethods、admin_color_scheme_picker 和 show_password_fields。 其他一切都必須用 jQuery 來完成。 用你的標簽替換 () your_lable

add_action('admin_init', 'user_profile_fields_disable');

function user_profile_fields_disable() {

    global $pagenow;

    // apply only to user profile or user edit pages
    if ($pagenow!=='profile.php' && $pagenow!=='user-edit.php') {
        return;
    }

    // do not change anything for the administrator
    if (current_user_can('administrator')) {
        return;
    }

    add_action( 'admin_footer', 'user_profile_fields_disable_js' );

}


/**
 * Disables selected fields in WP Admin user profile (profile.php, user-edit.php)
 */
function user_profile_fields_disable_js() {
?>
    <script>
        jQuery(document).ready( function($) {
            var fields_to_disable = ['your_lable', 'role'];
            for(i=0; i<fields_to_disable.length; i++) {
                if ( $('#'+ fields_to_disable[i]).length ) {
                    $('#'+ fields_to_disable[i]).attr("disabled", "disabled");
                }
            }
        });
    </script>
<?php
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM