If you’re looking to speed up your WordPress site without spending a dime, you’re in the right place. Follow the steps below, and make sure to use the appropriate code snippet based on whether your site uses Elementor or not.
Simply hover over the snippet and click the ‘Copy’ button to get started.
Before you apply any changes, we highly recommend backing up your site. This precaution ensures you can quickly recover if any issues arise, which might be caused by existing codes, plugins, or your current setup.
add_filter( 'elementor/frontend/print_google_fonts', '__return_false' );
function disable_google_fonts() {
return false;
}
add_filter( 'print_google_fonts', 'disable_google_fonts' );
define('WP_POST_REVISIONS', 3);
function custom_font_display( $current_value, $font_family, $data ) {
return 'swap';
}
add_filter( 'font_display', 'custom_font_display', 10, 3 );
add_filter( 'elementor_pro/custom_fonts/font_display', function( $current_value, $font_family, $data ) {
return 'swap';
}, 10, 3 );
/* Disable lazy loading for single image* */
function wphelp_no_lazy_load_id( $value, $image, $context ) {
if ( 'the_content' === $context ) {
$image_url = wp_get_attachment_image_url( 10576, 'large' );
if ( false !== strpos( $image, ' src="' . $image_url . '"' )) {
return false;
}
}
return $value;
}
add_filter( 'wp_img_tag_add_loading_attr', 'wphelp_no_lazy_load_id', 10576, 10576 );
add_filter( 'wp_lazy_loading_enabled', '__return_false' );
add_filter( 'the_content', 'add_image_dimensions' );
function add_image_dimensions( $content ) {
preg_match_all( '/]+>/i', $content, $images);
if (count($images) < 1)
return $content;
foreach ($images[0] as $image) {
preg_match_all( '/(alt|title|src|width|class|id|height)=("[^"]*")/i', $image, $img );
if ( !in_array( 'src', $img[1] ) )
continue;
if ( !in_array( 'width', $img[1] ) || !in_array( 'height', $img[1] ) ) {
$src = $img[2][ array_search('src', $img[1]) ];
$alt = in_array( 'alt', $img[1] ) ? ' alt=' . $img[2][ array_search('alt', $img[1]) ] : '';
$title = in_array( 'title', $img[1] ) ? ' title=' . $img[2][ array_search('title', $img[1]) ] : '';
$class = in_array( 'class', $img[1] ) ? ' class=' . $img[2][ array_search('class', $img[1]) ] : '';
$id = in_array( 'id', $img[1] ) ? ' id=' . $img[2][ array_search('id', $img[1]) ] : '';
list( $width, $height, $type, $attr ) = getimagesize( str_replace( "\"", "" , $src ) );
$image_tag = sprintf( '', $src, $alt, $title, $class, $id, $width, $height );
$content = str_replace($image, $image_tag, $content);
}
}
return $content;
}
/**
* We will Dequeue the jQuery UI script as example.
*
* Hooked to the wp_print_scripts action, with a late priority (99),
* so that it is after the script was enqueued.
*/
function wp_remove_scripts() {
// check if user is admin
if (current_user_can( 'update_core' )) {
return;
}
else {
// Check for the page you want to target
if ( is_page( 'homepage' ) ) {
// Remove Scripts
wp_dequeue_style( 'jquery-ui-core' );
}
}
}
add_action( 'wp_enqueue_scripts', 'wp_remove_scripts', 99 );
/**
* Convert Uploaded Images to WebP Format
*
* This snippet converts uploaded images (JPEG, PNG, GIF) to WebP format
* automatically in WordPress. Ideal for use in a theme's functions.php file,
* or with plugins like Code Snippets or WPCodeBox.
*
* @package WordPress_Custom_Functions
* @author Mark Harris
* @link www.christchurchwebsolutions.co.uk
*
* Usage Instructions:
* - Add this snippet to your theme's functions.php file, or add it as a new
* snippet in Code Snippets or WPCodeBox.
* - The snippet hooks into WordPress's image upload process and converts
* uploaded images to the WebP format.
*
* Optional Configuration:
* - By default, the original image file is deleted after conversion to WebP.
* If you prefer to keep the original image file, simply comment out or remove
* the line '@unlink( $file_path );' in the wpturbo_handle_upload_convert_to_webp function.
* This will preserve the original uploaded image file alongside the WebP version.
*/
add_filter( 'wp_handle_upload', 'wpturbo_handle_upload_convert_to_webp' );
function wpturbo_handle_upload_convert_to_webp( $upload ) {
if ( $upload['type'] == 'image/jpeg' || $upload['type'] == 'image/png' || $upload['type'] == 'image/gif' ) {
$file_path = $upload['file'];
// Check if ImageMagick or GD is available
if ( extension_loaded( 'imagick' ) || extension_loaded( 'gd' ) ) {
$image_editor = wp_get_image_editor( $file_path );
if ( ! is_wp_error( $image_editor ) ) {
$file_info = pathinfo( $file_path );
$dirname = $file_info['dirname'];
$filename = $file_info['filename'];
// Create a new file path for the WebP image
$new_file_path = $dirname . '/' . $filename . '.webp';
// Attempt to save the image in WebP format
$saved_image = $image_editor->save( $new_file_path, 'image/webp' );
if ( ! is_wp_error( $saved_image ) && file_exists( $saved_image['path'] ) ) {
// Success: replace the uploaded image with the WebP image
$upload['file'] = $saved_image['path'];
$upload['url'] = str_replace( basename( $upload['url'] ), basename( $saved_image['path'] ), $upload['url'] );
$upload['type'] = 'image/webp';
// Optionally remove the original image
@unlink( $file_path );
}
}
}
}
return $upload;
}
/*
Plugin Name: Purge Cache
Description: Adds a button to the WordPress dashboard to clear the object cache
*/
add_action( 'admin_bar_menu', 'add_purge_cache_button', 999 );
function add_purge_cache_button( $wp_admin_bar ) {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$args = array(
'id' => 'purge-cache',
'title' => 'Purge Cache',
'href' => '#',
'meta' => array( 'class' => 'purge-cache' )
);
$wp_admin_bar->add_node( $args );
}
add_action( 'admin_footer', 'add_purge_cache_script' );
function add_purge_cache_script() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
?>
function exclude_specific_css_files($src, $handle) {
// List of CSS file URLs to exclude from minification
$excluded_css_files = array(
'/wp-content/plugins/elementor/assets/css/frontend-lite.min.css',
'/wp-content/plugins/elementor-pro/assets/css/frontend-lite.min.css',
);
// Check if the current CSS file URL matches any of the excluded URLs
foreach ($excluded_css_files as $excluded_css_file) {
if (strpos($src, $excluded_css_file) !== false) {
return $src; // Return the original unminified CSS file
}
}
// If the CSS file is not in the excluded list, proceed with minification
return minify_css_content($src);
}
function minify_css_content($content) {
$content = preg_replace('/\s+/', ' ', $content); // Remove extra whitespaces
$content = str_replace(array("\r\n", "\r", "\n", "\t"), '', $content); // Remove newlines and tabs
return $content;
}
add_filter('style_loader_src', 'exclude_specific_css_files', 10, 2);