WordPress is a powerful platform, but with a few strategic tweaks, you can make it even better. In this post, I’m going to share ten essential code snippets that you can add to your functions.php file to enhance your WordPress site. These snippets cover a range of enhancements from improving security and performance to enhancing the user experience and adding new functionalities.

1. Disable the WordPress Admin Bar for Non-Admins
The WordPress admin bar is a useful tool for admins, but it can be distracting for non-admin users. By adding the following code snippet, you can easily disable the admin bar for all users except administrators:
// Disable Admin Bar for Non-Admins
add_action('after_setup_theme', 'remove_admin_bar');
function remove_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
show_admin_bar(false);
}
}
This tweak will clean up the front-end for your regular users, offering them a distraction-free browsing experience.
2. Remove WordPress Version Number
You may want to remove the generator tag from the page head. Other files and responses can still reveal the version, so hiding this tag is not a substitute for updates.
// Remove WordPress Version Number
remove_action('wp_head', 'wp_generator');
This only removes the generator tag. It is not a security boundary: keep WordPress and extensions patched, use appropriate access controls and maintain backups.
3. Disable WordPress Emoji Script
WordPress loads emoji-related scripts and styles by default, even if your site doesn’t use them. This can slow down your site’s loading time. If you don’t need emojis, you can disable these unnecessary scripts with the following code:
// Disable WordPress Emoji Scriptremove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
This small tweak can help speed up your site, contributing to a better user experience.
4. Limit Post Revisions
Limiting future revisions can reduce stored history. Put the following constant in wp-config.php before WordPress is loaded, not in functions.php. It does not remove existing revisions.
// Limit Post Revisions
define('WP_POST_REVISIONS', 5); // You can change the number as needed
This snippet helps ensure that your database remains optimized without sacrificing the safety net of revisions.
5. Enable Featured Image Support for Posts and Pages
If your theme doesn’t support featured images by default, you might be missing out on a great way to make your content more visually appealing. The following snippet will enable featured image support for your posts and pages:
// Enable Featured Image Support
add_theme_support('post-thumbnails');
Now, you can easily add eye-catching images to your posts, making them more engaging for your readers.
6. Disable XML-RPC for Enhanced Security
The following filter disables XML-RPC methods that require authentication. It does not disable pingbacks or every unauthenticated XML-RPC method; check whether remote publishing or other integrations rely on it.
// Disable XML-RPC
add_filter('xmlrpc_enabled', '__return_false');
Review the documented scope of xmlrpc_enabled before using this setting.
7. Enable SVG Support
SVGs (Scalable Vector Graphics) are a popular format for icons and logos because they are lightweight and scalable without losing quality. However, WordPress doesn’t support SVG uploads by default. Here’s how to enable SVG support:
// Enable SVG Support
function add_svg_to_upload_mimes($upload_mimes) {
$upload_mimes['svg'] = 'image/svg+xml';
return $upload_mimes;
}
add_filter('upload_mimes', 'add_svg_to_upload_mimes');
The MIME filter alone does not sanitize SVGs. SVG files can contain active content; use a maintained sanitizer such as Safe SVG and restrict who can upload them.
8. Custom Excerpt Length
The default excerpt length in WordPress is 55 words, but sometimes you might want to customize it to better fit your theme or content style. You can adjust the excerpt length with this snippet:
// Custom Excerpt Length
function custom_excerpt_length($length) {
return 20; // Change this number to set your desired excerpt length
}
add_filter('excerpt_length', 'custom_excerpt_length', 999);
This code gives you more control over how much content is displayed in excerpts on your site.
9. Redirect Users After Login
If you want to redirect users to a specific page after they log in, you can do so with the following snippet:
// Redirect Users After Login
function custom_login_redirect($redirect_to, $request, $user) {
// Is there a user to check?
if (isset($user->roles) && is_array($user->roles)) {
// Redirect based on role
if (in_array('administrator', $user->roles)) {
return admin_url(); // Redirect to the dashboard for admins
} else {
return home_url(); // Redirect to homepage for other users
}
}
return $redirect_to;
}
add_filter('login_redirect', 'custom_login_redirect', 10, 3);
This snippet allows you to direct users to different pages based on their role after they log in.
10. Remove jQuery Migrate
jQuery Migrate supports older JavaScript integrations. Only remove it after checking the theme and plugins for deprecated API usage; a smaller dependency list does not establish a measured performance improvement.
// Remove jQuery Migrate
function remove_jquery_migrate($scripts) {
if (!is_admin() && isset($scripts->registered['jquery'])) {
$script = $scripts->registered['jquery'];
if ($script->deps) { // Check whether the script has any dependencies
$script->deps = array_diff($script->deps, array('jquery-migrate'));
}
}
}
add_action('wp_default_scripts', 'remove_jquery_migrate');
Test menus, forms, galleries and third-party widgets after changing the dependency. Restore it if compatibility errors appear.
How to Safely Add These Snippets
Before you start adding these snippets to your functions.php file, here are a few tips:
- Backup Your Site: Always backup your site before making changes to the
functions.phpfile. You can use a plugin or manually back up your files and database. - Use a child theme or a small site-specific plugin for customizations, so an update does not overwrite them. Work in a local or staging copy before deploying.
- Add ordinary PHP snippets inside the PHP context. Omit a closing PHP tag in a PHP-only file. The revision constant is the exception: configure it in wp-config.php before WordPress loads.
Conclusion
Customizing WordPress is one of its core strengths, allowing you to tailor your site to your exact needs. These 10 snippets are just a starting point, offering simple yet powerful ways to enhance your site’s functionality, performance, and security. Whether you’re a seasoned developer or just getting started, these tweaks can help you get the most out of your WordPress site. Happy customizing!
Pick the customization you need
These are independent examples, not a checklist to enable on every site. Check a PHP snippet with php -l, test the affected behaviour and keep a rollback copy. For editor presentation, see our block-style guide.



