All WordPress snippets
Want your own copy?
Fork this repo and build your own snippet collection. Fork on GitHub →
WordPress
Hide Admin Menu Items Based on Email Domain
Trims down the wp-admin sidebar for anyone whose account email isn't on your company's domain — useful for client accounts that shouldn't see internal tools.
php
/**
* Specify which users can see admin menu items based on their email address
*/
function remove_dashboard_menus() {
$user_data = get_userdata( get_current_user_id() );
$user_email = isset( $user_data->user_email ) ? $user_data->user_email : '';
if ( ! strpos( $user_email, '@yourcompany.com' ) ) {
// Hide Updates under Dashboard menu
remove_submenu_page( 'index.php', 'update-core.php' );
// Hide Comments
remove_menu_page( 'edit-comments.php' );
// Hide Plugins
remove_menu_page( 'plugins.php' );
// Hide Themes, Customizer and Widgets under Appearance menu
remove_submenu_page( 'themes.php', 'themes.php' );
remove_submenu_page( 'themes.php', 'customize.php?return=' . urlencode( $_SERVER['REQUEST_URI'] ) );
remove_submenu_page( 'themes.php', 'widgets.php' );
// Hide Tools
remove_menu_page( 'tools.php' );
// Hide General Settings
remove_menu_page( 'options-general.php' );
}
}
add_action( 'admin_menu', 'remove_dashboard_menus' );
More WordPress snippets
Register and Output a Custom Nav Menu
Adds a new menu location you can assign in Appearance → Menus, shows how to output it in a template, and includes the multi-menu version too.
Define a Reusable Global String
A simple pattern for storing a value — like a phone number or address — in one place and reusing it anywhere in your theme.
Auto-Escape HTML Typed Inside Code Tags
Makes sure any markup typed inside <code> or <tt> tags in the editor gets escaped automatically, so it displays as visible text instead of being rendered as real HTML.