Select Page

Remove query strings from static resources in WordPress

Use this to remove query strings from static resources in WordPress

// Remove query strings from static resources
function _remove_script_version( $src ){
	$parts = explode( '?ver', $src );
	return $parts[0];
}
add_filter( 'script_loader_src', '_remove_script_version', 15, 1 );
add_filter( 'style_loader_src', '_remove_script_version', 15, 1 );

Leverage Browser Caching in WordPress via .htaccess

Add this to your .htacess to Leverage Browser Caching

## https://crunchify.com/how-to-speed-up-wordpress-leveraging-browser-caching-via-htaccess/
## EXPIRES 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 application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##

<FilesMatch "\.(js|css|html|htm|php|xml)$">
SetOutputFilter DEFLATE
</FilesMatch>

<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>

How to delete all images from your WordPress site

Use this SQL to remove all images/documents/etc from your media library

1 – Run these in phpmyadmin

DELETE FROM wp_postmeta
WHERE post_id IN
(
SELECT id
FROM wp_posts
WHERE post_type = 'attachment'
)
;
DELETE FROM wp_posts WHERE post_type = 'attachment'

2 – Manually delete all the year folders in the /wp-content/uploads folder.

Hide premium WordPress theme notifications

Most premium themes ask you to install additional plugins however if you dont install all of the plugins it recommends and keep them activated you will see a warning at the top of the admin area which comes back even if you close it.

If you add this code to your functions file it will hide the box. You might need to change the id of setting-error-tgmpa however on two sites I am building this is the same id.

<?php 
//hide update notifications
add_action('admin_head', 'my_custom_fonts');
function my_custom_fonts() {
  echo '<style>
    #setting-error-tgmpa {
			display:none;      
    }
  </style>';
}
?>

Hide menu items in the WordPress Admin Area

Use this to hide menu items in the WordPress admin area that you do not use.

<?php 
function remove_menus_links(){
 remove_menu_page( 'edit.php?post_type=portfolio' );
 remove_menu_page( 'edit.php?post_type=team' );
 remove_menu_page( 'edit.php?post_type=clients' );
 remove_menu_page( 'edit.php?post_type=agni_slides' );
}
add_action( 'admin_menu', 'remove_menus_links' );
?>

Set a maximum image upload size in WordPress

This can be used to set a maximum image upload size in WordPress, this helps to stop clients uploading large image files.

This example is 1.5mb

<?php 
//limit image file size to 1.5mb
function whero_limit_image_size($file) {
// Calculate the image size in KB
$image_size = $file['size']/1024;
// File size limit in KB
$limit = 1500;
// Check if it's an image
$is_image = strpos($file['type'], 'image');
if ( ( $image_size > $limit ) && ($is_image !== false) )
        $file['error'] = 'Your picture is too large. It has to be smaller than '. $limit .'KB';
return $file;
}
add_filter('wp_handle_upload_prefilter', 'whero_limit_image_size');
?>

Move WooCommerce category description up to be full width

This will move the WooCommerce category description up to be full width instead of in the right hand column.

<?php 
//move cat description up the page
remove_action( 'woocommerce_archive_description', 'woocommerce_taxonomy_archive_description', 10 );
add_action( 'woocommerce_before_main_content', 'woocommerce_taxonomy_archive_description', 2 );
?>

Remove WooCommerce long description title ‘Product description’

Use this to remove the hardcoded WooCommerce long description title ‘Product description’

<?php 
//remove long desc title
add_filter( 'woocommerce_product_description_heading', 'remove_product_description_heading' );
function remove_product_description_heading() {
return '';
}
?>

Add from before variable prices on WooCommerce

Use this in your functions file to add from before variable prices on WooCommerce

<?php 
//add from before price
function wc_wc20_variation_price_format( $price, $product ) {
    // Main Price
    $prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
    $price = $prices[0] !== $prices[1] ? sprintf( __( 'from: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

    // Sale Price
    $prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
    sort( $prices );
    $saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'from: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

    if ( $price !== $saleprice ) {
        $price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
    }

    return $price;
}
add_filter( 'woocommerce_variable_sale_price_html', 'wc_wc20_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wc_wc20_variation_price_format', 10, 2 );
?>

Add WooCommerce parent category name as a class to body of child products

If you need to style category pages depending on category this will add the parent category name as a class into the body meaning that you can then target all child categories with a small amount of css.

<?php
//add woocommerce parent category name to body
function woo_custom_taxonomy_in_body_class( $classes ){
    $custom_terms = get_the_terms(0, 'product_cat');
    if ($custom_terms) {
      foreach ($custom_terms as $custom_term) {

        // Check if the parent category exists:
        if( $custom_term->parent > 0 ) {
            // Get the parent product category:
            $parent = get_term( $custom_term->parent, 'product_cat' );
            // Append the parent class:
            if ( ! is_wp_error( $parent ) )
                $classes[] = 'product_parent_cat_' . $parent->slug;
        }

        $classes[] = 'product_cat_' . $custom_term->slug;
      }
    }
    return $classes;
}
add_filter( 'body_class', 'woo_custom_taxonomy_in_body_class' );
?>

Archives