Free Code Snippets

Allow SVG through WordPress Media Uploader

Add the following to your theme’s functions.php file to allow .svg files to be uploaded through the WordPress media uploader:

function svg_mime_type( $mimes ){
	$mimes['svg'] = 'image/svg+xml';
	return $mimes;
add_filter( 'upload_mimes', 'svg_mime_type' );

Remove the WordPress Login Shake Effect

When you go to the WordPress login page and you or a member tries to login but the login fails, a failed box will appear and that box shakes. You can remove the shaking effect by adding the following to your theme’s functions.php file:

function remove_login_shake() {
        remove_action('login_head', 'wp_shake_js', 12);
add_action('login_head', 'remove_login_shake');

Display Content Only on First Page of Category

WordPress categories are designed to list posts in that category. This is limiting and you may want to display content on the first page of the category – an advertisements, information, related services, etc… Use the following code snippet to display the content only on the first page of a category:

if ( !is_paged() ){
	echo 'Content to be displayed only on the first page.';
} else {
	echo '';

Add the code snippet to a is_category conditional IF statement for certain categories and / or instead of echo, use include for a file.

Specify a Vary: Accept-Encoding header

This is a requirement for Google’s pagespeed. This is needed because there are bugs in some public proxies that may lead to compressed versions of your resources being served to users that don’t support compression. Specifying the Vary: Accept-Encoding header instructs the proxy to store both a compressed and uncompressed version of the resource.

Add the following to your .htaccess file:

<IfModule mod_headers.c>
  <FilesMatch "\.(js|css|xml|gz)$">
    Header append Vary: Accept-Encoding

Display Author’s Display Name

You can display the author’s display name, inside the loop, by using the following code snippet:

   echo 'Author: ' . get_the_author_meta( 'display_name' );

If you want to display the author’s display outside the loop, use the following code snippet:

   echo 'Author: ' . get_the_author_meta( 'display_name', $user_id );

Replace $user_id with the ID number of the user.

You can change the text before the author’s display name, by editing the content inside the single quotes of 'Author: '. If you wish to add text after the author’s display name, add . 'Ending text goes here' just before the semi-colon.

Change Default Theme For WordPress Multisites

When you create a new site in a WordPress multisite installation, the created site will use WordPress’s default theme, which most likely will be the newest theme they created. However, you may not like or want this. You can change the default theme a new multisite site uses by adding the following to your wp-config.php file:

define( 'WP_DEFAULT_THEME', 'custom-theme-name' );

Replace custom-theme-name with the name of the theme you want to be the default.

Increase or Decrease WordPress JPEG Quality

By default WordPress will automatically compress your JPEG quality to 90%. To increase or decrease the amount, use the following code snippet:

// Increase or decrease JPEG quality
function himpfen_jpeg_quality() {
	return 100;
add_filter( 'jpeg_quality', 'himpfen_jpeg_quality' );

Add the code snippet to your theme’s functions.php file.

On line 3, you’ll see the number 100. This is the percentage you want your jpeg quality to be. Change it to whatever you want – anything below 90 is a decrease in quality, whereas, anything over 90 is an increase in jpeg quality.

Force Trailing Slash

Add the following to your .htaccess file to force a / at the end of the URL.

<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} /+[^.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

Empty the Trash Bin Every X Days

WordPress features a trash bin, which was add in version 2.9. The trash bin contains any content that you have deleted and automatically empties every 30 days.

You can change the 30 days to another interval, by adding the following to your wp-config.php file.

define( 'EMPTY_TRASH_DAYS', 7 );

You trash bin will be automatically emptied every 7 days. Change the 7 to whatever you want.

By changing the 7 to a 0, the trash bin will be disabled and will be replaced with a delete permanently link.

Page 1 of 3123