Home Website Building Knowledge Standard Post

Standard Post

Disable WordPress's Default Feature of Generating Responsive Images

Earlier, I integrated Upyun acceleration for the website, using Upyun to speed up the WordPress site. As a result, I encountered a bug today. I added an image to an article where I disabled the new WordPress editor, and the image did not display. I saw that the image URL in the editor was replaced with Upyun's, but the image did not show on the front end. Checking the source code, I found that there were still blog resource links...

Updated on November 1, 2019 About 3 minutes reading
禁用WordPress默认生成响应式图片的功能

Previously, I integrated Upyun acceleration for the website,using Upyun to accelerate WordPress websites, as a result, I encountered a bug today.

I gaveDisabling the New WordPress Editor, but the image didn't display. I saw that the image address in the editing interface was replaced with Upyun's, but the image didn't show on the front end. Checking the source code revealed there were still blog resource links.

图片不显示

<p><img src="https://edgeone.naibabiji.com/wp-content/uploads/2019/04/naibabiji.com_2019-05-06_14-49-40.jpg" alt="禁用Wordpress新版编辑器" width="600" height="377" class="aligncenter size-full wp-image-744" srcset="https://edgeone.naibabiji.com/wp-content/uploads/2019/04/naibabiji.com_2019-05-06_14-49-40.jpg 600w, https://edgeone.naibabiji.com/wp-content/uploads/2019/04/naibabiji.com_2019-05-06_14-49-40-150x94.jpg 150w" sizes="(max-width: 600px) 100vw, 600px" /></p>

It can be seen that in the source code, the attributessrcset="" andsizes="" two attributes, the URL is the old address of the blog.

After searching, I found out that it's because WordPress added the automatic responsive image setting feature after version 4.4, and the Upyun plugin apparently cannot replace this part yet. So we need to disable this automatic responsive image attribute code.

Disable WordPress's Default Feature of Generating Responsive Images

Just add the following content to the functions file and save it.

//disable srcset on images
function disable_srcset( $sources ) {
return false;
}
add_filter( 'wp_calculate_image_srcset', 'disable_srcset' );

Additionally, Naiba also tested that as long as the image does not contain this class attribute, the responsive image attributes will not be generated.

class="aligncenter size-full wp-image-744"

Additionally, there is a corresponding WordPress pluginDisable Responsive Images Complete

Rate this article post
Previous Add ALT Attribute to Gravatar Avatar Images in WordPress Comments Continue reading content around the same timeline. Next Add specific text title display to inactive website tabs View the next related tutorial or experience.

AI Website Building Assistant

🤖
Hello! I am the Naibabiji AI Assistant. How can I help you?
Quick Consultation: