Archives: Snippets | Page 3

Remove jQuery Migrate

WordPress loads jQuery Migrate for backward compatibility. If your site and plugins support modern jQuery, you can remove Migrate and save an HTTP request. Add to your functions.php file. For more on performance optimization.

add_action( 'wp_default_scripts', function( $scripts ) {
	if ( ! is_admin() && isset( $scripts->registered['jquery'] ) ) {
		$script = $scripts->registered['jquery'];
		if ( $script->deps ) {
			$script->deps = array_diff( $script->deps, array( 'jquery-migrate' ) );
		}
	}
} );

Add Custom Image Sizes

You can define custom image sizes for your site. Images are generated on upload or when regenerating thumbnails. Add to your functions.php file. For more on image optimization.

add_action( 'after_setup_theme', function() {
	add_image_size( 'card-thumbnail', 400, 300, true );
	add_image_size( 'hero-banner', 1920, 600, true );
} );

Replace names and dimensions as needed. Use true for crop or false for scale.

Change Posts Per Page in Archives

WordPress defaults to 10 posts per page. You can change this for all archives or a specific one. Add to your functions.php file. For more on adding pagination.

add_action( 'pre_get_posts', function( $query ) {
	if ( ! is_admin() && $query->is_main_query() && $query->is_home() ) {
		$query->set( 'posts_per_page', 12 );
	}
} );

Replace is_home() with is_archive() or is_post_type_archive('your_cpt') as needed.

Remove WordPress Admin Footer Text

WordPress displays “Thank you for creating with WordPress” at the bottom of the admin. You can remove or replace the text. Add to your functions.php file. For more on WordPress hooks.

add_filter( 'admin_footer_text', '__return_empty_string' );
add_filter( 'update_footer', '__return_empty_string', 11 );

Limit Number of Post Revisions

WordPress saves revisions of every post by default. Limiting the number saves database space. Add to your wp-config.php file (before “That’s all, stop editing!”). For more on improving performance.

define( 'WP_POST_REVISIONS', 3 );

Set 0 to disable completely or a number (e.g. 3-5) to limit.

Remove Block Library CSS on Frontend

WordPress loads wp-block-library.css on every page, even when the site does not use blocks. If your theme does not use Gutenberg blocks, you can remove it. Add to your functions.php file. For more on CSS optimization.

add_action( 'wp_enqueue_scripts', function() {
	wp_dequeue_style( 'wp-block-library' );
	wp_dequeue_style( 'wp-block-library-theme' );
	wp_dequeue_style( 'global-styles' );
}, 100 );

Note: If the page has block content, the CSS may break.

Remove Version Query String from Scripts and Styles

WordPress adds ?ver=X.X to CSS and JS files. Removing the version parameter can improve caching. Add to your functions.php file. For more tips on removing scripts and styles.

add_filter( 'script_loader_src', function( $src ) {
	return remove_query_arg( 'ver', $src );
}, 15, 1 );
add_filter( 'style_loader_src', function( $src ) {
	return remove_query_arg( 'ver', $src );
}, 15, 1 );

Add loading=”lazy” to iframes

WordPress adds loading="lazy" to images by default, but not to iframes (YouTube, maps, etc.). The following code adds lazy loading to iframes. For more performance tips, see the Guide to Google PageSpeed for WordPress.

add_filter( 'the_content', function( $content ) {
	return preg_replace( '/<iframe/', '<iframe loading="lazy"', $content );
} );

Redirect 404 Errors to Homepage

If you want to redirect users who land on a non-existent page (404) to the homepage, add the following code. Note: Google recommends showing a custom 404 page. For more on what causes 404 errors and how to handle them.

add_action( 'template_redirect', function() {
	if ( is_404() ) {
		wp_redirect( home_url(), 302 );
		exit;
	}
} );