Skip to content

Instantly share code, notes, and snippets.

@tatemz
Last active December 19, 2017 20:38
Show Gist options
  • Save tatemz/8f083f2cc1bcfd581164ccae72db2c5b to your computer and use it in GitHub Desktop.
Save tatemz/8f083f2cc1bcfd581164ccae72db2c5b to your computer and use it in GitHub Desktop.
Fixing W3 Total Cache CDN with Beaver Builder CSS media
<?php
// W3 Total Cache Integration with Beaver Builder
add_filter( 'fl_builder_render_css', __NAMESPACE__ . '\bb_plugin_cdn_url' );
function bb_plugin_cdn_url( $css ) {
// Based on wp-content/plugins/w3-total-cache/extensions/WordPressSEO.php
if ( class_exists( 'W3_Plugin_CdnCommon' ) ) {
$site_url = wp_upload_dir();
$w3_plugin_cdncommon = w3_instance( 'W3_Plugin_CdnCommon' );
$cdn = $w3_plugin_cdncommon->get_cdn();
// If we are using a cdn with w3tc
if ( $cdn ) {
$parsed = parse_url( $site_url['baseurl'] );
$path = $parsed['path'];
// Grab the CDN path
$remote_path = $w3_plugin_cdncommon->uri_to_cdn_uri( $path );
// Format the CDN url
$new_url = $cdn->format_url( $remote_path );
// If we are not using a cdn with w3tc
} else {
// Don't format the css
return $css;
}
// Replace the uploads URLs with the new URL
return str_replace( $site_url['baseurl'], $new_url, $css );
}
// Don't format the css
return $css;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment