WordPress 5.8-隐藏或删除管理员个人资料页面中的个人字段

时间:2021-11-07 作者:Sukinoz

此主题有多个线程,但没有更新到Wordpress 5.8

问题是,2021 WP 5.8,what is the cleanest or recommended code to hide profile fields when creating/editing a user, 除了基本的(用户名、电子邮件、姓名、姓氏…)之外?

当我说;“推荐”;或“或”;“最干净”;我的意思是,例如,如果有可以删除的内容;通过挂钩;,比如;管理配色方案;,那么这是最好的选择,对吗?

其他wordpress版本的一些选项:

提前感谢。

1 个回复
SO网友:Sukinoz

这在5.8中对我有效工作于;“添加新用户”;和在“中”;编辑用户

  • “li>
  • ”;“公共显示名称”;使用插件进行管理
  • 代码:

    // Remove fields from Admin profile page
    if ( ! function_exists( \'cor_remove_personal_options\' ) ) {
        function cor_remove_personal_options( $subject ) {
            $subject = preg_replace(\'#<h2>\'.__("Personal Options").\'</h2>#s\', \'\', $subject, 1); // Remove the "Personal Options" title
            $subject = preg_replace(\'#<tr class="user-rich-editing-wrap(.*?)</tr>#s\', \'\', $subject, 1); // Remove the "Visual Editor" field
            $subject = preg_replace(\'#<tr class="user-comment-shortcuts-wrap(.*?)</tr>#s\', \'\', $subject, 1); // Remove the "Keyboard Shortcuts" field
            $subject = preg_replace(\'#<tr class="show-admin-bar(.*?)</tr>#s\', \'\', $subject, 1); // Remove the "Toolbar" field
            $subject = preg_replace(\'#<h2>\'.__("Name").\'</h2>#s\', \'\', $subject, 1); // Remove the "Name" title
            // $subject = preg_replace(\'#<tr class="user-display-name-wrap(.*?)</tr>#s\', \'\', $subject, 1); // "Display name publicly as" field (not working ok when editing user. Better remove and manage with "Force First and Last Name as Display Name" plugin
            $subject = preg_replace(\'#<h2>\'.__("Contact Info").\'</h2>#s\', \'\', $subject, 1); // Remove the "Contact Info" title
            $subject = preg_replace(\'#<tr class="user-url-wrap(.*?)</tr>#s\', \'\', $subject, 1); // Remove the "Website" field in EDIT USER
            $subject = preg_replace(\'#<th scope="row"><label for="url(.*?)</th>#s\', \'\', $subject, 1); // Remove the "Website" field in NEW USER (part 1)
            $subject = preg_replace(\'#<td><input name="url"(.*?)</td>#s\', \'\', $subject, 1); // Remove the "Website" field SUKINOZ in NEW USER (part 2)  
            $subject = preg_replace(\'#<h2>\'.__("About Yourself").\'</h2>#s\', \'\', $subject, 1); // Remove the "About Yourself" title
            $subject = preg_replace(\'#<h2>\'.__("About the user").\'</h2>#s\', \'\', $subject, 1); // Remove the "About the user" title      
            $subject = preg_replace(\'#<tr class="user-description-wrap(.*?)</tr>#s\', \'\', $subject, 1); // Remove the "Biographical Info" field
            $subject = preg_replace(\'#<tr class="user-profile-picture(.*?)</tr>#s\', \'\', $subject, 1); // Remove the "Profile Picture" field
            return $subject;
        }
    
        function cor_profile_subject_start() {
            if ( ! current_user_can(\'manage_options\') ) {
                ob_start( \'cor_remove_personal_options\' );
            }
        }
    
        function cor_profile_subject_end() {
            if ( ! current_user_can(\'manage_options\') ) {
                ob_end_flush();
            }
        }
    }
    add_action( \'admin_head\', \'cor_profile_subject_start\' );
    add_action( \'admin_footer\', \'cor_profile_subject_end\' );
    
    //Remove fields from Admin profile page via JS to hide nickname field which is mandatory
    function remove_personal_options(){
        if ( ! current_user_can(\'manage_options\') ) { // \'update_core\' may be more appropriate
            echo \'<script type="text/javascript">jQuery(document).ready(function($) {
                $(\\\'form#your-profile tr.user-nickname-wrap\\\').hide(); // Hide the "nickname" field
            });</script>\';
        }
    }
    add_action(\'admin_head\',\'remove_personal_options\');
    
    // Removes ability to change Theme color for the users
    remove_action( \'admin_color_scheme_picker\', \'admin_color_scheme_picker\' );