WordPress 4.7 custom background image bug & workaround

Gotta love Sarah, but there’s a small bug in Vaughan’s (WordPress 4.7) that breaks (part of the CSS) when Autoptimized. If you have a theme that supports custom backgrounds (e.g. TwentySixteen) and you set that custom background in the Customizer, the URL ends up escaped (well, they wp_json_encode() it actually) like this;

body{background-image: url("http:\/\/localhost\/wordpress\/wp-content\/uploads\/layerslider\/Full-width-demo-slider\/left.png");}

Which results in the Autoptimized CSS for the background-image being broken because the URL is not recognized as such. The bug has been confirmed already and the fix should land in WordPress 4.7.1.

If you’re impacted and can’t wait for 4.7.1 to be released, there are 2 workarounds:
1. simple: disable the “Aggregate inline CSS”-option
2. geeky: use this code snippet to fix the URL before AO has a change to misinterpret it;

add_filter('autoptimize_filter_html_before_minify','fix_encoded_urls');
function fix_encoded_urls($htmlIn) {
	if ( strpos($htmlIn,"body.custom-background") !== false ) {
		preg_match_all("#background-image:\s?url\(?[\"|']((http(s)?)?:\\\/\\\/.*)[\"|']\)?#",$htmlIn,$badUrls);
	  	if ($badUrls) {
			foreach ($badUrls[1] as $badUrl) {
				$htmlIn = str_replace($badUrl, stripslashes($badUrl), $htmlIn);
			}
		}
	}
  	return $htmlIn;
}

Music from Our Tube; James Bittersweet Lewis Trio

Just picked up on Worldwide FM; James Brandon Lewis Trio featuring Anthony Pirog and Nicholas Ryan Gant (aka @ghetto_falsetto). Kind of mellow-y, which I’m not always into, but duet of Lewis’ tenor-sax and the Gant’s falsetto scatting makes for a great mix, keeping the feeling fresh and real.

Bittersweet

Watch this video on YouTube.

And if you want to something more in-your-face; have a go at “No Filter” by the same trio. Just tenor-sax, bass & drums, but you’ll be headbanging as if you were on the front-row of a metal-festival.

So how does Autoptimize work anyway?

A question that has come up a couple of times already is how Autoptimize and it’s cache work. So let’s do some copy/pasting of what I replied earlier on the wordpress.org support forum;

  1. AO intercepts the HTML created by WordPress for a request (using the output buffer)
  2. all references to JS (and CSS) are extracted from the HTML
  3. all original references to JS (and CSS) are removed from the HTML (the code in the original files is left as is, AO never changes those files)
  4. all JS (and CSS) is aggregated (JS in one string, CSS in as many strings as there were media types)
  5. the md5-hash (mathematical/ cryptographic function that generates a quasi-unique string based on another string) of the aggregated JS (and CSS) is calculated
  6. using the md5 AO checks if a cached file with that md5 exists and if so continues to step 8
  7. if no cached file is found, the JS (and CSS) is minified and cached in a new file, with the md5 as part of the filename
  8. the links to the autoptimized JS (and CSS) file in cache are injected in the HTML
  9. the HTML is minified (but not cached in Autoptimize)
  10. the HTML is returned to WordPress (where it can be cached by a page cache and sent to the visitor)

This is especially interesting if you want to understand why the cache size can “explode”; if in step 4 the code is even a bit different from previous requests, the md5-hash in step 5 will be different so the file will not be found in cache (step 6) and the code will be re-minified (which is relatively expensive) and cached (step 7).

And that, my friends, is how Autoptimize works.

Music from Our Tube; Handsome Boy Modeling School ft. Roisin Murphy

It’s frustrating how much great music I just miss out on. Take this ‘ancient’ (as in last-century-ancient!) track by Handsome Boy Modeling School, “The Truth” featuring Roisin Murphy and J-live, which I did not even know existed until I heard it on Radio Nova a couple of days ago …

Handsome Boy Modeling School – The Truth (feat. Roisin of Moloko & J-Live)

Watch this video on YouTube.

How to fix CSS media-types impacting Autoptimized CSS order

pixeldima-forum-screenshotAdel of Pixeldima contacted me to look into a problem with some of the styles not being applied after autoptimization on the forum-page in his beautiful OKAB-theme. I had seen similar problems before, but decided to dig in this time to understand the root cause. That root cause turned out to be 2 CSS-files, one overriding the style of the other, having a different media-type, which messed up the load order once autoptimized. This can either be fixed at the theme-level, at AO configuration level or with a couple of lines of code hooking into AO’s API.

The problem

Without Autoptimize (which one can trigger by simply adding ?ao_noptimize=1 to the URL), these 2 relevant files were loaded (among many others obviously):

  • bbpress.css (the default bbpress css-file) was loaded first with media=”screen”
  • okab/bbpress-styles.css (part of the theme) is loaded second with media=”all”, overriding some of the CSS in bbpress.css

Autoptimize honors the media-types, resulting in 2 Autoptimized CSS-files;

  • autoptimize_1a002577cefe349767025a16201f37a9.css with media=”all” is loaded first (because the very first CSS-file encountered in the HTML had media=”all”) which contains okab/bbpress-styles.css
  • autoptimize_8e3c7dac90177214b6583286ddaa141f.css is loaded second with media=”screen”, containing bbpress.css

So due to the mediatype the code in bbpress-styles.css was loaded before the CSS in bbpress.css when Autoptimized, resulting in styles being overwritten the wrong way (bbpress-styles.css is overwritten by bbpress.css instead of the other way around).

The solution(s)

As Adel was the theme developer, he could immediately attack the root issue by enqueuing bbpress-styles.css with media=”screen”. This way both files share the same media-type and go in the same autoptimized CSS-file, resulting in the CSS of bbpress.css getting properly overwritten by the CSS of okab/bbpress-styles.css.

If you’re not the theme developer you’ll probably not want to change the theme’s code (although you could dequeue and re-enqueue in a child-theme’s functions.php), in which case you have two workarounds at your disposal: the easiest workaround being simply excluding the CSS-file that gets overwritten from CSS optimization (i.e. in this case simply adding okab/bbpress-style.css to the list of CSS optimization exclusions). Easy indeed, but on the downside you’ll have one non-optimized CSS-file. That’s why it’s a good thing we have our precious little API, allowing us to override the media-type of the offending file with just a couple of lines of code, like this;

add_filter('autoptimize_filter_css_tagmedia','check_mediatype',10,2);
function check_mediatype($media,$tag) {
  if ( strpos($tag,"okab/bbpress-styles.css") !== false ) {
	$media=array("screen");
  }
  return $media;
}

Conclusion

As Autoptimize honors the original CSS’s media-types, 2 CSS-files that ought to be loaded in a specific order might end up being loaded in the wrong order if their media-type is different. This can be fixed in the theme or worked around by excluding a specific CSS-file or using AO’s API to override the media-type for the offending CSS-file.