Top 10 Must Have Code Snippets to Supercharge Your WordPress Site

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

Exposing your WordPress version number can be a security risk, as it may provide hackers with information about potential vulnerabilities. Here’s how you can remove the WordPress version number from your site’s HTML output:

// Remove WordPress Version Number
remove_action('wp_head', 'wp_generator');

With this snippet, you can make it harder for malicious actors to target your site.

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

Post revisions are a great feature for content creators, but over time, they can bloat your database and slow down your site. Limiting the number of revisions WordPress saves can keep your database lean and efficient:

// 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

XML-RPC allows remote access to your WordPress site, but it’s also a frequent target for brute-force attacks. If you don’t need XML-RPC, it’s a good idea to disable it:

// Disable XML-RPC
add_filter('xmlrpc_enabled', '__return_false');

This additional layer of security can help protect your site from potential threats.

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');

This snippet allows you to upload SVG files directly into your WordPress media library.

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

WordPress includes a script called jQuery Migrate for backward compatibility, but it’s often unnecessary for modern sites and can slow down page loading. You can remove it with this snippet:

// 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');

Removing jQuery Migrate can help optimize your site’s performance.

How to Safely Add These Snippets

Before you start adding these snippets to your functions.php file, here are a few tips:

  1. Backup Your Site: Always backup your site before making changes to the functions.php file. You can use a plugin or manually back up your files and database.
  2. Access the functions.php File: You can edit the functions.php file by navigating to Appearance > Theme Editor in your WordPress dashboard. Alternatively, you can access the file via FTP, or if you are a developer do it in your git- codebase!
  3. Add the Snippets: Carefully add the snippets to the end of your functions.php file. Make sure not to remove the closing ?> tag if it exists.

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!

More tutorials

Stay updated with the latest from WpBlockz

* indicates required