Skip to content

Instantly share code, notes, and snippets.

@mannieschumpert
Last active August 12, 2019 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mannieschumpert/6105315 to your computer and use it in GitHub Desktop.
Save mannieschumpert/6105315 to your computer and use it in GitHub Desktop.
Remove user fields for specific user role in WordPress
<?php
// Hook into the admin footer
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 }
}
}
@jrevillini
Copy link

thanks! question: is there a reason you decided to use jquery over wp hooks? just easier?

@mannieschumpert
Copy link
Author

@jrevillini This gist is from 2013! 😱

I have no idea if WordPress hooks have evolved to include a simpler solution, but at the time using jQuery to remove fields was the only option. (This gist does use WordPress hooks, btw, as shown in line 3.)

@jrevillini
Copy link

haha yeah i commented before i really looked into it. i rewrote it completely but the code put me in the right direction. i also enqueued css to hide the fields before JS could strip them. supposedly you can figure out all the little hooks and filters to take out the various fields, but it's quite a rabbit hole you have to go down to find everything!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment