Can't find retina file with custom UPLOADS constant
-
We have a wordpress setup based on a boilerplate from Git. This works perfect, except for the retina images in combination with rewrite and srcset options. After some debugging this is caused by files which can’t be found on the disk by the wp-retina-2x plugin.
It is caused by a specific local configuration file which overrides the UPLOADS and WP_SITEURL constants. The image urls as generated by WordPress look like this:
https://royalfish.local/wordpress/../uploads/2013/10/logo2-51x73.png
. With the current algorithm the local file can’t be found, since it looks in theABSPATH . 'wordpress/../uploads/2013/10/logo2-51x73.png'
directory.Supposing that there are more people with a similar problem, I created a patch for this as follows (visually: https://note.io/1bzIUO4):
diff --git wp-content/plugins/wp-retina-2x/wp-retina-2x.php wp-content/plugins/wp-retina-2x/wp-retina-2x.php index 96f1d8589fee98ddf3bde5635e4c1e1901cc90d0..b76179a13ed0597c5e6b925bf19fb408fca3f8e5 100644 --- wp-content/plugins/wp-retina-2x/wp-retina-2x.php +++ wp-content/plugins/wp-retina-2x/wp-retina-2x.php @@ -107,8 +107,7 @@ function wr2x_srcset_rewrite( $buffer ) { @$doc->loadHTML( mb_convert_encoding( $buffer, 'HTML-ENTITIES', 'UTF-8' ) ); // = ($doc->strictErrorChecking = false;) $imageTags = $doc->getElementsByTagName('img'); foreach ( $imageTags as $tag ) { - $img_info = parse_url( $tag->getAttribute('src') ); - $img_pathinfo = ltrim( $img_info['path'], '/' ); + $img_pathinfo = wr2x_get_pathinfo_from_image_src($tag->getAttribute('src')); $filepath = trailingslashit( ABSPATH ) . $img_pathinfo; $potential_retina = wr2x_get_retina( $filepath ); if ( $potential_retina != null ) { @@ -145,8 +144,7 @@ function wr2x_html_rewrite( $buffer ) { @$doc->loadHTML( $buffer ); // = ($doc->strictErrorChecking = false;) $imageTags = $doc->getElementsByTagName('img'); foreach ( $imageTags as $tag ) { - $img_info = parse_url( $tag->getAttribute('src') ); - $img_pathinfo = ltrim( $img_info['path'], '/' ); + $img_pathinfo = wr2x_get_pathinfo_from_image_src($tag->getAttribute('src')); $filepath = trailingslashit( ABSPATH ) . $img_pathinfo; $potential_retina = wr2x_get_retina( $filepath ); if ( $potential_retina != null ) { @@ -164,6 +162,17 @@ function wr2x_html_rewrite( $buffer ) { * */ +function wr2x_get_pathinfo_from_image_src($image_src) { + $site_url = trailingslashit(site_url()); + if (strpos($image_src, $site_url) === 0) { + return substr($image_src, strlen($site_url)); + } + else { + $img_info = parse_url( $image_src ); + return ltrim( $img_info['path'], '/' ); + } +} + // UPDATE THE ISSUE STATUS OF THIS ATTACHMENT function wr2x_update_issue_status( $attachmentId, $issues = null, $info = null ) { if ( wr2x_is_ignore( $attachmentId ) )
Can you include this (or similar code) to be able to be upgrade proof again?
- The topic ‘Can't find retina file with custom UPLOADS constant’ is closed to new replies.