10+ Useful Code Snippets to Add to WordPress function.php File for Speed Up and Optimization

WordPress is a powerful content management system, but it can be even more powerful with the help of code snippets. Code snippets are small pieces of PHP code that can be added to your WordPress theme’s functions.php file to extend its functionality (using a child theme is preferred).

In this blog post, we will share 10+ useful code snippets you can add to your WordPress Function.php file to fix common issues and speed up WordPress and Woocommerce.

How to Disable/Hide the Admin Toolbar?

The admin toolbar is a great way to quickly access your WordPress dashboard, but it can also be a distraction. If you don’t use the admin toolbar, you can disable it with the following code snippet:

// Disable the admin toolbar
add_filter('show_admin_bar', '__return_false');

How to Disable/Hide the Admin Toolbar for Non-Admin Users?

The following code snippet can be added to hide the admin bar for all users except administrators:

// Hide admin bar for non-admins
function blorax_hide_admin_bar() {
  if (!current_user_can('administrator')) {
    add_filter('show_admin_bar', '__return_false');
  }
}
add_action('init', 'blorax_hide_admin_bar');

How to Show Post Thumbnails in RSS Feeds?

By default, WordPress does not show post thumbnails in RSS feeds. If you want to show post thumbnails in your RSS feeds, you can use the following code snippet:

// Show post thumbnails in RSS feeds
add_filter('the_excerpt_rss', 'blorax_post_thumbnail_in_rss', 10, 1);
function blorax_post_thumbnail_in_rss($content) {
  global $post;
  if (has_post_thumbnail($post->ID)) {
    $content .= get_the_post_thumbnail($post->ID, 'thumbnail', array('class' => 'wp-post-image'));
  }
  return $content;
}

How To Remove/Hide URL Field From Comment Form?

Detailed information about importance of hiding that field you can read on [Solved]: How to Remove Website URL Field from the WordPress Comment Form?. To hide it just add the following code:

add_filter('comment_form_default_fields', 'blorax_unset_url_field');
function blorax_unset_url_field($fields){
    if(isset($fields['url']))
       unset($fields['url']);
       return $fields;
}

How to Change the Read More Text for Excerpts?

The default read more text for excerpts is “Read more”. If you want to change the read more text, you can use the following code snippet:

// Change the read more text for excerpts
function blorax_read_more_text($more) {
  return 'Continue reading...';
}
add_filter('excerpt_more', 'blorax_read_more_text');

How to Change the Post Excerpt Length?

The default post excerpt length is 55 words. If you want to change the post excerpt length, you can use the following code snippet:

// Change the post excerpt length
function blorax_excerpt_length($length) {
  return 30;
}
add_filter('excerpt_length', 'blorax_excerpt_length');

How to Add an Admin User with PHP?

If you need to add an admin user to your WordPress site, you can use the following code snippet:

// Add an admin user with PHP
function blorax_add_admin_user($username, $password, $email) {
  $user_data = array(
    'username' => $username,
    'password' => $password,
    'email' => $email,
    'role' => 'administrator',
  );
  wp_insert_user($user_data);
}

blorax_add_admin_user('admin', 'password', '[email protected]');

How to Enable Shortcodes in Text Widgets?

By default, shortcodes are not enabled in text widgets. If you want to enable shortcodes in text widgets, you can use the following code snippet:

// Enable shortcodes in text widgets
add_filter('widget_text', 'do_shortcode');

If you want to add a custom dashboard logo to your WordPress site, you can use the following code snippet:

// Add a custom dashboard logo
function blorax_custom_dashboard_logo() {
  echo '<style>
    .wrap .logo {
      background-image: url(/path-to-logo/custom-logo.png);
    }
  </style>';
}
add_action('admin_head', 'blorax_custom_dashboard_logo');

How to Allow SVG Upload?

By default, WordPress does not allow SVG files to be uploaded. If you want to allow SVG files to be uploaded, you can use the following code snippet:

// Allow SVG upload
function blorax_allow_svg_upload($mime_types) {
  $mime_types['svg'] = 'image/svg+xml';
  return $mime_types;
}
add_filter('upload_mimes', 'blorax_allow_svg_upload');

How to Disable XML-RPC in WordPress?

The XML-RPC protocol is a way to connect to WordPress from other applications. However, the XML-RPC protocol can also be used by attackers to hack your WordPress site. If you don’t need to use the XML-RPC protocol, you can disable it with the following code snippet:

// Disable XML-RPC in WordPress
define('WP_XMLRPC_ENABLED', false);

How to Remove jQuery Migrate?

jQuery Migrate is a plugin that helps to maintain compatibility with older versions of jQuery. However, jQuery Migrate can also cause performance problems. If you don’t need to use jQuery Migrate, you can remove it with the following code snippet:

// Remove jQuery Migrate
remove_action('wp_enqueue_scripts', 'wp_migrate_scripts');

Conclusion

These are just a few of the many useful code snippets that you can add to your WordPress Function.php file. By using code snippets, you can customize your WordPress site to your exact needs.

Please note! Take a FULL Backup of your website before ANY optimization steps and TEST it.

We hope you find these code snippets useful!

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *