正規表現を使用する以外に、WordPressが注目の画像の高さと幅の属性を自動的にハードコーディングする簡単な方法があるかどうか疑問に思っています...
私は自分のプロジェクトに柔軟なグリッドを使用しているため(そうではありません!)、これによりファンキーな画像の問題が発生しています。
正規表現を使用する以外に、WordPressが注目の画像の高さと幅の属性を自動的にハードコーディングする簡単な方法があるかどうか疑問に思っています...
私は自分のプロジェクトに柔軟なグリッドを使用しているため(そうではありません!)、これによりファンキーな画像の問題が発生しています。
回答:
注目の画像URLを取得して、コンテンツに手動で追加できます。例:
<?php 
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'thumbnail' ); 
if ($image) : ?>
    <img src="<?php echo $image[0]; ?>" alt="" />
<?php endif; ?> srcset属性を含まないため、WP 4.4以降のレスポンシブイメージを防止します。
                    でimage_downsize見つかった関数の出力をフィルタリングすることにより、幅と高さの属性を削除できますwp-includes/media.php。これを行うには、独自の関数を作成し、テーマのfunctions.phpファイルを介して、またはプラグインとして実行します。
例:
widthおよびheight属性を削除します。
/**
 * This is a modification of image_downsize() function in wp-includes/media.php
 * we will remove all the width and height references, therefore the img tag 
 * will not add width and height attributes to the image sent to the editor.
 * 
 * @param bool false No height and width references.
 * @param int $id Attachment ID for image.
 * @param array|string $size Optional, default is 'medium'. Size of image, either array or string.
 * @return bool|array False on failure, array on success.
 */
function myprefix_image_downsize( $value = false, $id, $size ) {
    if ( !wp_attachment_is_image($id) )
        return false;
    $img_url = wp_get_attachment_url($id);
    $is_intermediate = false;
    $img_url_basename = wp_basename($img_url);
    // try for a new style intermediate size
    if ( $intermediate = image_get_intermediate_size($id, $size) ) {
        $img_url = str_replace($img_url_basename, $intermediate['file'], $img_url);
        $is_intermediate = true;
    }
    elseif ( $size == 'thumbnail' ) {
        // Fall back to the old thumbnail
        if ( ($thumb_file = wp_get_attachment_thumb_file($id)) && $info = getimagesize($thumb_file) ) {
            $img_url = str_replace($img_url_basename, wp_basename($thumb_file), $img_url);
            $is_intermediate = true;
        }
    }
    // We have the actual image size, but might need to further constrain it if content_width is narrower
    if ( $img_url) {
        return array( $img_url, 0, 0, $is_intermediate );
    }
    return false;
}新しい関数をimage_downsizeフックに添付します。
/* Remove the height and width refernces from the image_downsize function.
 * We have added a new param, so the priority is 1, as always, and the new 
 * params are 3.
 */
add_filter( 'image_downsize', 'myprefix_image_downsize', 1, 3 );また、CSSで画像を正しくスケーリングすることを忘れないでください:
/* Image sizes and alignments */
.entry-content img,
.comment-content img,
.widget img {
    max-width: 97.5%; /* Fluid images for posts, comments, and widgets */
}
img[class*="align"],
img[class*="wp-image-"] {
    height: auto; /* Make sure images with WordPress-added height and width attributes are scaled correctly */
}
img.size-full {
    max-width: 97.5%;
    width: auto; /* Prevent stretching of full-size images with height and width attributes in IE8 */
}これがお役に立てば幸いです。
乾杯、
srcset、sizes残念ながら、その他のレスポンシブ画像の属性が削除されます。:(これは私の現在のソリューションで、属性を再構築します:gist.github.com/cibulka/8e2bf16b0f55779af590472ae1bf9239
                    インラインスタイル/属性を無効にすることができます!important:
.wp-post-image {
    width: auto !important; /* or probably 100% in case of a grid */
    height: auto !important; 
}これは最もクリーンなソリューションではありませんが、問題を解決します。
wp-post-imageが私の画像に含まれていませんでした。代わりに私のようなものがありましたwp-image-26。別のセレクターを使用する必要がありましたが、アイデアはうまくいきました。
                    CSSソリューション:
img[class*="align"], img[class*="wp-image-"] {
    width: auto;
    height: auto;
}これにより、レスポンシブ画像が正常に機能する一方で、img要素のwidth属性とheight属性を維持できます。これはおそらく、古いブラウザー、パフォーマンス、および/またはHTMLバリデーターを渡すのに適しています。
PHPソリューション:
これにより、WPエディターで(「メディアの追加」を介して)新しく追加されたメディアに幅/高さ属性が追加されなくなります。参考までに、画像のキャプションにも影響する場合があります!
function remove_widthHeight_attribute( $html ) {
   $html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
   return $html;
}
add_filter( 'post_thumbnail_html', 'remove_widthHeight_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_widthHeight_attribute', 10 );