11 Most Useful Code Snippets for WordPress

How to add browser cache to WordPress?

One of the easiest ways to speed up loading of your website is to set up browser cache headers. That means you have to instruct user’s browser how should it deal with resources, like images, HTML files, stylesheets, scripts and more.

When you activate it, the browser will start to remember which resources have already been loaded and will not load them again and again. The “remembering” process will be done on the first view of a website, so it may take a little longer.

If you want to enable browser cache for your wordpress website (or any other), put the code below into your .htaccess file. Feel free to modify the access times, but the default ones are fine enough.

# Browser caching
<IfModule mod_expires.c>
   ExpiresActive On
   ExpiresByType image/jpg "access 1 year"
   ExpiresByType image/jpeg "access 1 year"
   ExpiresByType image/gif "access 1 year"
   ExpiresByType image/png "access 1 year"
   ExpiresByType text/css "access 1 month"
   ExpiresByType text/html "access 1 month"
   ExpiresByType application/pdf "access 1 month"
   ExpiresByType text/x-javascript "access 1 month"
   ExpiresByType image/x-icon "access 1 year"
   ExpiresDefault "access 1 month"
</IfModule>

How to add favicon in WordPress?

If you want to add a favicon to your WordPress website, you can either use a plugin or a simple code that will add some code to the <head> section of your page. You will often get your favicons from a favicon generator like this one, so this is what to put into your functions.php file.

(all the favicon files should be put in the main directory of your WordPress site, that means next to files like wp-config.php)

// Add custom favicons
function percival_add_custom_favicons() { ?>
   <link rel="apple-touch-icon" sizes="180x180" href="<?php echo get_site_url() ?>/apple-touch-icon.png">
   <link rel="icon" type="image/png" sizes="32x32" href="<?php echo get_site_url() ?>/favicon-32x32.png">
   <link rel="icon" type="image/png" sizes="16x16" href="<?php echo get_site_url() ?>/favicon-16x16.png">
   <link rel="manifest" href="<?php echo get_site_url() ?>/site.webmanifest">
   <link rel="mask-icon" href="<?php echo get_site_url() ?>/safari-pinned-tab.svg" color="#2086ca">
   <meta name="msapplication-TileColor" content="#ffffff">
   <meta name="theme-color" content="#ffffff">
<?php }
add_action('wp_head', 'percival_add_custom_favicons');

How to redirect new registered user to a specific page in WordPress?

If you want to redirect a newly registered user to a specific page, use the snippet below (functions.php)

function percival_registration_redirect(){
   return home_url( '/thank-you/' );
}
add_filter( 'registration_redirect', 'percival_registration_redirect' );

How to move all scripts into footer in WordPress?

It’s a good practice to keep your stylesheets in header and javascript files (scripts) in the footer. It not only speeds up the load time of your website, but also doesn’t block it from rendering.

Here is how to move all scripts into footer in WordPress (functions.php)

if (!is_admin()){
   remove_action('wp_head', 'wp_print_scripts');
   remove_action('wp_head', 'wp_print_head_scripts', 9);
   remove_action('wp_head', 'wp_enqueue_scripts', 1);

   add_action('wp_footer', 'wp_print_scripts', 5);
   add_action('wp_footer', 'wp_enqueue_scripts', 5);
   add_action('wp_footer', 'wp_print_head_scripts', 5);
}

How to remove WordPress header tags?

WordPress puts many tags in header, like generator tag. If you want to remove them, use the following code (functions.php)

// Remove wp_head links
function percival_clean_wp_header() {
   remove_action('wp_head', 'wp_generator');
   remove_action('wp_head', 'rel_canonical');
   remove_action('wp_head', 'rsd_link');
   remove_action('wp_head', 'wlwmanifest_link');
   remove_action('wp_head', 'feed_links',2);
   remove_action('wp_head', 'feed_links_extra',3);
   remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
   remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
}
add_action('init', 'percival_clean_wp_header');

How to remove WordPress version (generator tag)?

In terms of security, it’s nice NOT to show your visitors what WordPress version you are running.

By default, WordPress uses a generator tag to show it.

If you want to remove WordPress version from showing up in your source code, use the following snippet:

function percival_remove_generator() { 
   return '';
} 
add_filter( 'the_generator', 'percival_remove_generator' );

How to change the excerpt text and length in WordPress?

If you want to change excerpt text or length in WordPress, here is how to do it (functions.php).

New excerpt length

// New excerpt length
function percival_new_excerpt_length($length) { 
   return 40; 
} 
add_filter('excerpt_length', 'percival_new_excerpt_length');

New excerpt text

function percival_new_excerpt_more($more) { 
   return '...'; 
} 
add_filter('excerpt_more', 'percival_new_excerpt_more');

How to adjust autosave time in WordPress?

If you want to adjust post or page autosave time, use the code below (wp-config.php)

# Autosave every 5 minutes (300 seconds)
define('AUTOSAVE_INTERVAL', 300);

How to disable or limit post revisions in WordPress?

Every time you save a page or post in WordPress, it creates a revision. They are definitely helpful if you need to go back to a previous version of your article.

However, if your website is getting bigger and bigger (let’s say 2000 articles and growing), each revision is going to take its place in your database. That means lots of table rows that you don’t really need, but your website performance can be hit too.

Here is how you can disable or limit post revisions in WordPress (put the lines below in your wp-config.php file)

// Disable post revisions 
define('WP_POST_REVISIONS', false); 

// Limit post revisions to a maximum of 5 for a post 
define('WP_POST_REVISIONS', 5);

How to disable search function in WordPress?

Use the code snippet below to disable search function in WordPress (functions.php)

function percival_filter_query( $query, $error = true ) {
   if ( is_search() ) {
      $query->is_search = false;
      $query->query_vars['s'] = false;
      $query->query['s'] = false;

      if ( $error == true ) {
         $query->is_404 = true;
      }
   }
}

add_action( 'parse_query', 'percival_filter_query' );
add_filter( 'get_search_form', function() { return null; } );

How to disable Admin Bar in WordPress?

WordPress Admin Bar is a place where you can quickly use some of the most popular functions, like add new posts, pages, media elements.

However, there are times you don’t want to show it. For example, you want to hide it for specific users, especially those with lower permissions.

Here is how you can disable admin bar in WordPress.

Copy the following code snippet and paste it in your theme’s functions.php file.

// Remove the admin bar
add_filter( 'show_admin_bar', '__return_false' );

Leave a Comment

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

0 Shares
Tweet
Share
Share
Reddit