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 );
} );
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;
}
} );
Sometimes you want search engines not to index a specific post type (e.g. landing pages). The following code adds noindex, nofollow to a specific CPT. For more on preventing search results from being indexed.
add_action( 'wp_head', function() {
if ( is_singular( 'your_post_type' ) ) {
echo '<meta name="robots" content="noindex, nofollow">' . "n";
}
} );
Replace your_post_type with your CPT slug.
HPOS (High-Performance Order Storage) improves order performance in WooCommerce. You can enable it via code. For more on enabling HPOS in WooCommerce.
add_action( 'before_woocommerce_init', function() {
if ( class_exists( AutomatticWooCommerceUtilitiesFeaturesUtil::class ) ) {
AutomatticWooCommerceUtilitiesFeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
}
} );
Then enable it under WooCommerce > Settings > Advanced > Features.
If you want to use the Classic Editor for a specific post type, add the following code. For more on creating Custom Post Types.
add_filter( 'use_block_editor_for_post_type', function( $use, $post_type ) {
if ( $post_type === 'your_post_type' ) {
return false;
}
return $use;
}, 10, 2 );
Replace your_post_type with your CPT slug.
WordPress loads oEmbed scripts for embedding content from YouTube, Twitter, etc. If you do not use embeds, remove them. For more performance tips, see the Guide to Google PageSpeed for WordPress.
remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
remove_action( 'wp_head', 'wp_oembed_add_host_js' );
remove_action( 'rest_api_init', 'wp_oembed_register_route' );
add_filter( 'embed_oembed_discover', '__return_false' );
add_filter( 'oembed_dataparse', '__return_false' );
WordPress adds logged-in to the body when a user is logged in, but only in the admin. The following code adds a class on the frontend too. Useful for custom styling or showing different content. For more on WordPress hooks and filters.
add_filter( 'body_class', function( $classes ) {
if ( is_user_logged_in() ) {
$classes[] = 'logged-in-user';
}
return $classes;
} );
WordPress loads emoji scripts (wp-emoji-release.min.js, etc.) on every page by default. Most sites do not need this, and the scripts add unnecessary HTTP requests and a bit of overhead.
To remove the emoji scripts, add the following code to your functions.php file:
/**
* Remove WordPress emoji scripts
*/
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
add_filter( 'tiny_mce_plugins', function( $plugins ) {
return is_array( $plugins ) ? array_diff( $plugins, array( 'wpemoji' ) ) : array();
} );
Result: fewer HTTP requests and lighter pages. For more performance tips, see the Guide to Google PageSpeed for WordPress Users.
The WordPress Heartbeat API sends AJAX requests to the server every 15-60 seconds (depending on context – editor, dashboard, etc.). It is useful for auto-save and session management, but on shared hosting it can cause high CPU load. For more ways to improve your site speed, see the Guide to Google PageSpeed for WordPress Users.
If you want to slow down the Heartbeat or disable it completely, add the following code to your functions.php file:
/**
* Disable or slow down WordPress Heartbeat API
*/
add_action( 'init', function() {
// Option 1: Disable Heartbeat completely
wp_deregister_script( 'heartbeat' );
// Option 2: Slow down Heartbeat to 60 seconds (uncomment and remove Option 1)
// add_filter( 'heartbeat_settings', function( $settings ) {
// $settings['interval'] = 60;
// return $settings;
// } );
}, 1 );
Note: Fully disabling Heartbeat may affect auto-save in the post editor. If you need auto-save, use Option 2 (slow down to 60 seconds) instead of full disable.
To allow SVG file uploads through the media library, add the following code to your functions.php file:
function cc_mime_types($mimes) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
add_filter('upload_mimes', 'cc_mime_types');
A more detailed explanation can be found in this post.