{"id":256,"date":"2026-04-15T08:51:01","date_gmt":"2026-04-15T08:51:01","guid":{"rendered":"https:\/\/anubhavtiwari.dev\/blog\/?p=256"},"modified":"2026-04-23T07:28:58","modified_gmt":"2026-04-23T07:28:58","slug":"auto-convert-images-to-webp-wordpress-without-plugin","status":"publish","type":"post","link":"https:\/\/anubhavtiwari.dev\/blog\/auto-convert-images-to-webp-wordpress-without-plugin\/","title":{"rendered":"Automatically Convert Uploaded Images to WebP in WordPress (Without Plugin)"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p>Website speed matters a lot for both users and search engines. One of the best ways to improve performance is by using WebP images instead of traditional formats like JPG and PNG.<\/p>\n\n\n\n<p>In this guide, you\u2019ll learn how to automatically convert uploaded images to WebP in WordPress using a simple code snippet.<br><br>Why Use WebP Images?<\/p>\n\n\n\n<p>Improved user experience<\/p>\n\n\n\n<p>Smaller file size compared to JPG\/PNG<\/p>\n\n\n\n<p>Faster page loading speed<\/p>\n\n\n\n<p>Better SEO rankings<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How It Works<\/h3>\n\n\n\n<p>We\u2019ll add a custom function in WordPress that converts images into WebP format automatically whenever you upload them.<br><br><br><br>\/**<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Convert uploaded images to WebP format automatically.<br>*\/<br>add_filter(&#8216;wp_handle_upload&#8217;, &#8216;convert_to_webp_on_upload&#8217;, 10, 2);<\/li>\n<\/ul>\n\n\n\n<p>function convert_to_webp_on_upload($upload, $context) {<br>\/\/ Check if the upload was successful and if it&#8217;s an image<br>if (!isset($upload[&#8216;file&#8217;]) || strpos($upload[&#8216;type&#8217;], &#8216;image\/&#8217;) !== 0) {<br>return $upload;<br>}<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$file_path = $upload&#91;'file'];\n$file_type = $upload&#91;'type'];\n\n\/\/ Avoid double conversion or converting already WebP files\nif ($file_type === 'image\/webp') {\n    return $upload;\n}\n\n\/\/ Load the image based on its type\nswitch ($file_type) {\n    case 'image\/jpeg':\n        $image = imagecreatefromjpeg($file_path);\n        break;\n    case 'image\/png':\n        $image = imagecreatefrompng($file_path);\n        imagepalettetotruecolor($image); \/\/ Preserve transparency\n        imagealphablending($image, true);\n        imagesavealpha($image, true);\n        break;\n    case 'image\/gif':\n        $image = imagecreatefromgif($file_path);\n        break;\n    default:\n        return $upload;\n}\n\nif (!$image) {\n    return $upload; \/\/ Return original if image creation fails\n}\n\n\/\/ Define the new file path with .webp extension\n$webp_path = preg_replace('\/\\.(jpe?g|png|gif)$\/i', '.webp', $file_path);\n\n\/\/ Convert and save as WebP (Quality: 80)\nif (imagewebp($image, $webp_path, 80)) {\n    \/\/ Remove the original file to save space\n    unlink($file_path);\n\n    \/\/ Update the upload array with new file details\n    $upload&#91;'file'] = $webp_path;\n    $upload&#91;'url']  = str_replace(basename($file_path), basename($webp_path), $upload&#91;'url']);\n    $upload&#91;'type'] = 'image\/webp';\n}\n\n\/\/ Free up memory\nimagedestroy($image);\n\nreturn $upload;<\/code><\/pre>\n\n\n\n<p>}<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Website speed matters a lot for both users and search engines. One of the best ways to improve performance is by using WebP images instead of traditional formats like JPG and PNG. In this guide, you\u2019ll learn how to automatically convert uploaded images to WebP in WordPress using a simple code snippet. Why Use [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":260,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[112,1],"tags":[],"class_list":["post-256","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-it-services","category-technology"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Automatically Convert Uploaded Images to WebP in WordPress<\/title>\n<meta name=\"description\" content=\"Learn how to automatically convert uploaded images to WebP format in WordPress without using any plugin. Improve website speed and SEO with a simple functions.php code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/anubhavtiwari.dev\/blog\/auto-convert-images-to-webp-wordpress-without-plugin\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Automatically Convert Uploaded Images to WebP in WordPress\" \/>\n<meta property=\"og:description\" content=\"Learn how to automatically convert uploaded images to WebP format in WordPress without using any plugin. Improve website speed and SEO with a simple functions.php code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/anubhavtiwari.dev\/blog\/auto-convert-images-to-webp-wordpress-without-plugin\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-04-15T08:51:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-23T07:28:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/anubhavtiwari.dev\/blog\/wp-content\/uploads\/2026\/04\/convert-images-to-webp-wordpress-without-plugin.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1536\" \/>\n\t<meta property=\"og:image:height\" content=\"1024\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Anubhav Tiwari\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Anubhav Tiwari\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/anubhavtiwari.dev\/blog\/auto-convert-images-to-webp-wordpress-without-plugin\/\",\"url\":\"https:\/\/anubhavtiwari.dev\/blog\/auto-convert-images-to-webp-wordpress-without-plugin\/\",\"name\":\"Automatically Convert Uploaded Images to WebP in WordPress\",\"isPartOf\":{\"@id\":\"https:\/\/anubhavtiwari.dev\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/anubhavtiwari.dev\/blog\/auto-convert-images-to-webp-wordpress-without-plugin\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/anubhavtiwari.dev\/blog\/auto-convert-images-to-webp-wordpress-without-plugin\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/anubhavtiwari.dev\/blog\/wp-content\/uploads\/2026\/04\/convert-images-to-webp-wordpress-without-plugin.webp\",\"datePublished\":\"2026-04-15T08:51:01+00:00\",\"dateModified\":\"2026-04-23T07:28:58+00:00\",\"author\":{\"@id\":\"https:\/\/anubhavtiwari.dev\/blog\/#\/schema\/person\/e427973c306e35da9874678360b9226f\"},\"description\":\"Learn how to automatically convert uploaded images to WebP format in WordPress without using any plugin. Improve website speed and SEO with a simple functions.php code.\",\"breadcrumb\":{\"@id\":\"https:\/\/anubhavtiwari.dev\/blog\/auto-convert-images-to-webp-wordpress-without-plugin\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/anubhavtiwari.dev\/blog\/auto-convert-images-to-webp-wordpress-without-plugin\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/anubhavtiwari.dev\/blog\/auto-convert-images-to-webp-wordpress-without-plugin\/#primaryimage\",\"url\":\"https:\/\/anubhavtiwari.dev\/blog\/wp-content\/uploads\/2026\/04\/convert-images-to-webp-wordpress-without-plugin.webp\",\"contentUrl\":\"https:\/\/anubhavtiwari.dev\/blog\/wp-content\/uploads\/2026\/04\/convert-images-to-webp-wordpress-without-plugin.webp\",\"width\":1536,\"height\":1024,\"caption\":\"Automatically Convert Uploaded Images to WebP in WordPress (Without Plugin)\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/anubhavtiwari.dev\/blog\/auto-convert-images-to-webp-wordpress-without-plugin\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/anubhavtiwari.dev\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Automatically Convert Uploaded Images to WebP in WordPress (Without Plugin)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/anubhavtiwari.dev\/blog\/#website\",\"url\":\"https:\/\/anubhavtiwari.dev\/blog\/\",\"name\":\"Blog\",\"description\":\"Anubhav Tiwari\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/anubhavtiwari.dev\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/anubhavtiwari.dev\/blog\/#\/schema\/person\/e427973c306e35da9874678360b9226f\",\"name\":\"Anubhav Tiwari\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/anubhavtiwari.dev\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/95c45b41431f9c91f7ec9ce2a116c0792b799b9e2139e485da90043f0c219450?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/95c45b41431f9c91f7ec9ce2a116c0792b799b9e2139e485da90043f0c219450?s=96&d=mm&r=g\",\"caption\":\"Anubhav Tiwari\"},\"sameAs\":[\"https:\/\/anubhavtiwari.dev\/blog\"],\"url\":\"https:\/\/anubhavtiwari.dev\/blog\/author\/anubhavtiwari-at72gmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Automatically Convert Uploaded Images to WebP in WordPress","description":"Learn how to automatically convert uploaded images to WebP format in WordPress without using any plugin. Improve website speed and SEO with a simple functions.php code.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/anubhavtiwari.dev\/blog\/auto-convert-images-to-webp-wordpress-without-plugin\/","og_locale":"en_US","og_type":"article","og_title":"Automatically Convert Uploaded Images to WebP in WordPress","og_description":"Learn how to automatically convert uploaded images to WebP format in WordPress without using any plugin. Improve website speed and SEO with a simple functions.php code.","og_url":"https:\/\/anubhavtiwari.dev\/blog\/auto-convert-images-to-webp-wordpress-without-plugin\/","og_site_name":"Blog","article_published_time":"2026-04-15T08:51:01+00:00","article_modified_time":"2026-04-23T07:28:58+00:00","og_image":[{"width":1536,"height":1024,"url":"https:\/\/anubhavtiwari.dev\/blog\/wp-content\/uploads\/2026\/04\/convert-images-to-webp-wordpress-without-plugin.webp","type":"image\/webp"}],"author":"Anubhav Tiwari","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Anubhav Tiwari","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/anubhavtiwari.dev\/blog\/auto-convert-images-to-webp-wordpress-without-plugin\/","url":"https:\/\/anubhavtiwari.dev\/blog\/auto-convert-images-to-webp-wordpress-without-plugin\/","name":"Automatically Convert Uploaded Images to WebP in WordPress","isPartOf":{"@id":"https:\/\/anubhavtiwari.dev\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/anubhavtiwari.dev\/blog\/auto-convert-images-to-webp-wordpress-without-plugin\/#primaryimage"},"image":{"@id":"https:\/\/anubhavtiwari.dev\/blog\/auto-convert-images-to-webp-wordpress-without-plugin\/#primaryimage"},"thumbnailUrl":"https:\/\/anubhavtiwari.dev\/blog\/wp-content\/uploads\/2026\/04\/convert-images-to-webp-wordpress-without-plugin.webp","datePublished":"2026-04-15T08:51:01+00:00","dateModified":"2026-04-23T07:28:58+00:00","author":{"@id":"https:\/\/anubhavtiwari.dev\/blog\/#\/schema\/person\/e427973c306e35da9874678360b9226f"},"description":"Learn how to automatically convert uploaded images to WebP format in WordPress without using any plugin. Improve website speed and SEO with a simple functions.php code.","breadcrumb":{"@id":"https:\/\/anubhavtiwari.dev\/blog\/auto-convert-images-to-webp-wordpress-without-plugin\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/anubhavtiwari.dev\/blog\/auto-convert-images-to-webp-wordpress-without-plugin\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/anubhavtiwari.dev\/blog\/auto-convert-images-to-webp-wordpress-without-plugin\/#primaryimage","url":"https:\/\/anubhavtiwari.dev\/blog\/wp-content\/uploads\/2026\/04\/convert-images-to-webp-wordpress-without-plugin.webp","contentUrl":"https:\/\/anubhavtiwari.dev\/blog\/wp-content\/uploads\/2026\/04\/convert-images-to-webp-wordpress-without-plugin.webp","width":1536,"height":1024,"caption":"Automatically Convert Uploaded Images to WebP in WordPress (Without Plugin)"},{"@type":"BreadcrumbList","@id":"https:\/\/anubhavtiwari.dev\/blog\/auto-convert-images-to-webp-wordpress-without-plugin\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/anubhavtiwari.dev\/blog\/"},{"@type":"ListItem","position":2,"name":"Automatically Convert Uploaded Images to WebP in WordPress (Without Plugin)"}]},{"@type":"WebSite","@id":"https:\/\/anubhavtiwari.dev\/blog\/#website","url":"https:\/\/anubhavtiwari.dev\/blog\/","name":"Blog","description":"Anubhav Tiwari","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/anubhavtiwari.dev\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/anubhavtiwari.dev\/blog\/#\/schema\/person\/e427973c306e35da9874678360b9226f","name":"Anubhav Tiwari","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/anubhavtiwari.dev\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/95c45b41431f9c91f7ec9ce2a116c0792b799b9e2139e485da90043f0c219450?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/95c45b41431f9c91f7ec9ce2a116c0792b799b9e2139e485da90043f0c219450?s=96&d=mm&r=g","caption":"Anubhav Tiwari"},"sameAs":["https:\/\/anubhavtiwari.dev\/blog"],"url":"https:\/\/anubhavtiwari.dev\/blog\/author\/anubhavtiwari-at72gmail-com\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/anubhavtiwari.dev\/blog\/wp-content\/uploads\/2026\/04\/convert-images-to-webp-wordpress-without-plugin.webp",1536,1024,false],"thumbnail":["https:\/\/anubhavtiwari.dev\/blog\/wp-content\/uploads\/2026\/04\/convert-images-to-webp-wordpress-without-plugin-150x150.webp",150,150,true],"medium":["https:\/\/anubhavtiwari.dev\/blog\/wp-content\/uploads\/2026\/04\/convert-images-to-webp-wordpress-without-plugin-300x200.webp",300,200,true],"medium_large":["https:\/\/anubhavtiwari.dev\/blog\/wp-content\/uploads\/2026\/04\/convert-images-to-webp-wordpress-without-plugin-768x512.webp",768,512,true],"large":["https:\/\/anubhavtiwari.dev\/blog\/wp-content\/uploads\/2026\/04\/convert-images-to-webp-wordpress-without-plugin-1024x683.webp",1024,683,true],"1536x1536":["https:\/\/anubhavtiwari.dev\/blog\/wp-content\/uploads\/2026\/04\/convert-images-to-webp-wordpress-without-plugin.webp",1536,1024,false],"2048x2048":["https:\/\/anubhavtiwari.dev\/blog\/wp-content\/uploads\/2026\/04\/convert-images-to-webp-wordpress-without-plugin.webp",1536,1024,false]},"uagb_author_info":{"display_name":"Anubhav Tiwari","author_link":"https:\/\/anubhavtiwari.dev\/blog\/author\/anubhavtiwari-at72gmail-com\/"},"uagb_comment_info":0,"uagb_excerpt":"Introduction Website speed matters a lot for both users and search engines. One of the best ways to improve performance is by using WebP images instead of traditional formats like JPG and PNG. In this guide, you\u2019ll learn how to automatically convert uploaded images to WebP in WordPress using a simple code snippet. Why Use&hellip;","_links":{"self":[{"href":"https:\/\/anubhavtiwari.dev\/blog\/wp-json\/wp\/v2\/posts\/256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/anubhavtiwari.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anubhavtiwari.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anubhavtiwari.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/anubhavtiwari.dev\/blog\/wp-json\/wp\/v2\/comments?post=256"}],"version-history":[{"count":3,"href":"https:\/\/anubhavtiwari.dev\/blog\/wp-json\/wp\/v2\/posts\/256\/revisions"}],"predecessor-version":[{"id":262,"href":"https:\/\/anubhavtiwari.dev\/blog\/wp-json\/wp\/v2\/posts\/256\/revisions\/262"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/anubhavtiwari.dev\/blog\/wp-json\/wp\/v2\/media\/260"}],"wp:attachment":[{"href":"https:\/\/anubhavtiwari.dev\/blog\/wp-json\/wp\/v2\/media?parent=256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anubhavtiwari.dev\/blog\/wp-json\/wp\/v2\/categories?post=256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anubhavtiwari.dev\/blog\/wp-json\/wp\/v2\/tags?post=256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}