根据已编辑用户的角色编辑个人资料页面字段

时间:2019-12-02 16:42:31

标签: php wordpress

我想自定义WordPress在编辑特定用户角色(例如订户)时管理员看到的Profile页面的外观。我发现了:

add_action( 'admin_footer-user-edit.php', 'remove_user_fields' );
function remove_user_fields(){

// Set desired user role to target
$role = 'subscriber';
// Check for user ID query string
if ( isset($_GET['user_id']) ) {
// Get data for user currently being edited
$id = filter_var($_GET['user_id'], FILTER_SANITIZE_NUMBER_INT);
$meta = get_user_meta($id);
$roles = unserialize($meta['wp_capabilities'][0]);

// Check if user has chosen role
if ( !empty($roles[$role]) ) {

  // Print jQuery that removes unneeded elements
  ?>
  <script type="text/javascript">
    jQuery(document).ready( function($) {
      // the following IDs are the input elements of the unneeded fields
      var ids = ['#rich_editing', // Rich editing button
                 '#admin_color_classic', // Admin color scheme
                 '#comment_shortcuts', // Keyboard shortcuts for comment moderation
                 '#aim', // AOL instant messenger
                 '#yim', // Yahoo messenger
                 '#jabber', // Jabber
                 '#description']; // User bio
      for (var i = 0; i < ids.length; i++) {
        $(ids[i]).closest('tr').remove();
      }
    });
  </script>

  <?php }
 }
}    

,但这仅在登录用户具有此角色时才起作用。我希望管理员用户角色的任何订阅者用户的个人资料页面上都隐藏一些字段,而不是其他角色(例如他自己的管理员角色)上的字段隐藏。

0 个答案:

没有答案
相关问题