Hide-my-WordPress & Autoptimize compatibility glue

An “Autoptimize Critical CSS“-user saw some weirdness in how the site looked when optimized. The reason for this turned out to be not the critical CSS, but the fact that he used “Hide My WordPress Pro”, a plugin that changes well-known paths in WordPress URL’s to other paths (e.g. /wp-content/plugins -> /modules/ and /wp-content/themes -> /templates) and uses rewrite rules .htaccess to map requests to the filesystem. This resulted in Autoptimize not finding the files on the filesystem (as AO does not “see” the mapping in .htaccess), leaving them un-aggregated.
To fix something like that, a small code snippet that hooks into Autoptimize’s API can do the trick;

add_filter('autoptimize_filter_cssjs_alter_url', 'rewrite_hidemywp');
function rewrite_hidemywp($url) {
    if ( strpos( $url, 'modules/' ) !== false && strpos( $url, 'wp-content' ) === false ) {
        $url = str_replace('modules/','wp-content/plugins/', $url);
    } elseif ( strpos( $url, 'templates/' ) !== false && strpos( $url, 'wp-content' ) === false ) {
	$url = str_replace('templates/','wp-content/themes/', $url);
    }
    return $url;
}

The above is just an example (as in the Pro version of hide-my-wp you can set paths of your own liking and you can even replace theme names by random-ish strings), but with a small amount of PHP-skills you should be able to work out the solution for your own site. Happy optimized hiding!

7 thoughts on “Hide-my-WordPress & Autoptimize compatibility glue”

  1. hi Frank, Thank you for your excellent work. Can I customize the cache storage path? I don’t want it to be placed in the wp-content/cache. I want it to be placed in the root directory of the website

    Reply
      • yes๏ผŒi find it yesterday.
        define(‘AUTOPTIMIZE_CACHE_CHILD_DIR’,’/resources/’);
        but, Can I change the directory & filename in website root?
        e.g. /cache/autoptimize/js/
        not /wp-content/cache/autoptimize/js/
        I haven’t tried
        thank you

        Reply

Leave a Reply to lee Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.