Top 10 Code Snippets to Supercharge Your WordPress Site

Top 10 Code Snippets to Supercharge Your WordPress Site

Top 10 Code Snippets

WordPress is a powerful platform, but sometimes it needs a little customization to meet specific needs. This is where the Code Snippets plugin shines. It allows you to safely add PHP code snippets to your site without directly editing your theme files, which can be risky and might get overwritten during updates. With this plugin, you can enable or disable snippets easily, keeping your customizations organized and your site functioning smoothly. In this article, we’ll explore 10 essential code snippets you can add to your WordPress site using the Code Snippets plugin to enhance its functionality and performance.

What is the Code Snippets Plugin?

The Code Snippets plugin is a user-friendly alternative to adding custom PHP code directly to your theme’s functions.php file. It offers a clean interface for managing snippets, supports categorization, and helps prevent fatal errors by providing an easy way to enable or disable individual snippets.

Key benefits of using the Code Snippets plugin:

  1. Error Management: Easily deactivate a problematic snippet without breaking your site.
  2. Portability: Snippets remain active even if you switch themes.
  3. Safety: Avoid editing core WordPress files, reducing the risk of issues during updates.
  4. Organization: Name and describe each snippet to keep track of what they do.

Related : Why Shopify May Not Be the Best Choice

Top 10 Code Snippets to Use

Here’s a list of the most useful code snippets to make your WordPress site faster, more secure, and tailored to your needs.

1. Disable WordPress Admin Bar for Non-Admins

For non-admin users, the WordPress admin bar can be distracting. This snippet disables it for everyone except administrators.

add_action('after_setup_theme', function() {

if (!current_user_can('administrator') && !is_admin()) {


show_admin_bar(false);


}


});

Why use it?
It improves the front-end experience for your users, especially if your site has a membership or e-commerce feature.

2. Redirect Users After Login

Direct users to different pages based on their roles after login.

add_filter('login_redirect', function($redirect_to, $request, $user) {

if (isset($user->roles) && is_array($user->roles)) {


if (in_array('subscriber', $user->roles)) {


return home_url('/subscriber-dashboard/');


} elseif (in_array('administrator', $user->roles)) {


return admin_url();


}


}


return $redirect_to;


}, 10, 3);

Use case:
Redirect subscribers to a dedicated dashboard or welcome page, creating a more personalized experience.

3. Disable WordPress Emojis

WordPress loads emoji scripts even if you don’t use them, slowing down your site. Disable them with this snippet.

add_action('init', function() {

remove_action('wp_head', 'print_emoji_detection_script', 7);


remove_action('wp_print_styles', 'print_emoji_styles');


remove_action('admin_print_scripts', 'print_emoji_detection_script');


remove_action('admin_print_styles', 'print_emoji_styles');


add_filter('tiny_mce_plugins', function($plugins) {


return is_array($plugins) ? array_diff($plugins, ['wpemoji']) : [];


});


add_filter('emoji_svg_url', '__return_false');


});

4. Disable Gutenberg Block Editor

Prefer the classic editor over Gutenberg? This snippet ensures Gutenberg is turned off.

add_filter('use_block_editor_for_post', '__return_false', 10);

Bonus:
Pair this with the Classic Editor plugin for complete control over your editing environment.

5. Increase WordPress Memory Limit

If you’re running resource-heavy plugins or themes, memory exhaustion errors can become an issue. Fix it with this code.

define('WP_MEMORY_LIMIT', '256M');

define('WP_MAX_MEMORY_LIMIT', '512M');

6. Add Google Analytics

Track your website visitors by embedding Google Analytics tracking code directly into your theme header.

add_action('wp_head', function() {

echo "
<!-- Google Analytics -->
<script async src='https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-X'></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXXX-X');
</script>"
;


});

7. Remove Query Strings from Static Resources

Speed up your site and improve caching by removing query strings from static resources.

add_filter('style_loader_src', 'remove_query_strings', 10, 2);
add_filter('script_loader_src', 'remove_query_strings', 10, 2);
function remove_query_strings($src) {
return remove_query_arg(‘ver’, $src);
}

8. Disable XML-RPC

XML-RPC is often exploited for DDoS attacks. Disable it with this snippet.

add_filter('xmlrpc_enabled', '__return_false');

Enhance your branding by replacing the default WordPress login page logo with your own.

add_action('login_enqueue_scripts', function() {

echo "
<style>
#login h1 a {
background-image: url('/path-to-your-logo.png');
background-size: contain;
width: 100%;
height: 84px;
}
</style>"
;


});

10. Schedule Automatic Database Optimization

Keep your database optimized without manual intervention.

if (!wp_next_scheduled('optimize_database_daily')) {
wp_schedule_event(time(), 'daily', 'optimize_database_daily');
}
add_action(‘optimize_database_daily’, function() {

global $wpdb;
$wpdb->query(‘OPTIMIZE TABLE ‘ . $wpdb->prefix . ‘options’);
$wpdb->query(‘OPTIMIZE TABLE ‘ . $wpdb->prefix . ‘posts’);
$wpdb->query(‘OPTIMIZE TABLE ‘ . $wpdb->prefix . ‘postmeta’);
});

 

How to Use These Snippets

  1. Install the Code Snippets plugin from the WordPress repository.
  2. Go to Snippets > Add New in your dashboard.
  3. Paste the code, name your snippet, and activate it.

By leveraging these top 10 code snippets, you can transform your WordPress site without relying on extra plugins. Each snippet is designed to solve a specific problem, making your site faster, safer, and more user-friendly.

 If you found this article helpful, don’t forget to share it. For any questions, leave a comment on our Ask Us page, and we’ll respond promptly!

اترك ردّاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *