Height and width of the img label removed by WordPress

Source: Internet
Author: User

Requirements

For example, on a desktop device, the image uses the following HTML code:

The Code is as follows:
1

On the mobile device side, because the screen is relatively small, if you want to make the image adaptive screen, we should remove the width and height attributes, or the image may be larger than the screen:

The Code is as follows:
1


Method 1,

Http://www.111cn.net/wy/wordpress/58923.htm
Copy the following code to the functions. php file of the current topic:

The Code is as follows:
Add_filter ('Post _ thumbnail_html ', 'remove _ width_attribute', 10 );
Add_filter ('image _ send_to_editor ', 'remove _ width_attribute', 10 );

Function remove_width_attribute ($ html ){
$ Html = preg_replace ('/(width | height) = "\ d *" \ s/', "", $ html );
Return $ html;
}

Method 2

The Code is as follows:
// Delete the width and height of the adaptive image, by Ludou
Function ludou_remove_width_height_attribute ($ content ){
Preg_match_all ("/<[img | IMG]. *? Src = [\ '| \ "] (. *? (? : [\. Gif | \. jpg | \. png \. bmp]) [\ '| \ "]. *? [\/]?> /", $ Content, $ images );
If (! Empty ($ images )){
Foreach ($ images [0] as $ index => $ value ){
$ New_img = preg_replace ('/(width | height) = "\ d *" \ s/', "", $ images [0] [$ index]);
$ Content = str_replace ($ images [0] [$ index], $ new_img, $ content );
}
}
Return $ content;
}
// Determine whether the browser is a mobile device
If (wp_is_mobile ()){
// Delete the width and height attributes of img in the document content
Add_filter ('the _ content', 'ludou _ remove_width_height_attribute ', 99 );
}

In this way, I will try again to see if the desired result is reached.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.