Otimização de Velocidade de Sites WordPress

Remover Google Fonts (otimização Elementor)

				
					add_filter( 'elementor/frontend/print_google_fonts', '__return_false' );
				
			

Reduzir revisões para 3 (otimização WordPress)

				
					define('WP_POST_REVISIONS', 3);
				
			

Assegurar que a fonte personalizada esteja carregada (otimização Elementor)

				
					add_filter( 'elementor_pro/custom_fonts/font_display', function( $current_value, $font_family, $data ) {
	return 'swap';
}, 10, 3 );
				
			

Interromper o Lazy Load

				
					add_filter( 'wp_lazy_loading_enabled', '__return_false' );
				
			

Remover JavaScript não utilizado

				
					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 );
				
			

Remover CSS não utilizado (pode pular esse código se estiver usando o PhastPress).

				
					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);
				
			

Converter imagens para WebP quando adicionadas à biblioteca.

				
					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;
}
				
			

Posts relacionados

Descubra como adicionar códigos CSS no WordPress de forma gratuita apenas com o Elementor na versão gratuita.

Preencha com suas informações