add_action('customize_register', 'theme_footer_customizer');
function theme_footer_customizer($wp_customize){
// Get upload directory
$upload_dir = wp_get_upload_dir();
// Create style file path
$style_file_path = sprintf( '%1$s/css', untrailingslashit( $upload_dir['basedir'] ) );
// Create directories if they do not exist
if( ! file_exists( $style_file_path ) ) {
wp_mkdir_p( $style_file_path );
}
// Sanitize contents ( probably needs to be sanitized better, maybe with a CSS specific library )
$contents = wp_filter_nohtml_kses( wp_strip_all_tags( wp_get_custom_css() ) );
// Replace the contents of the file with the new saved contents.
$style_file = $style_file_path . '/my.css';
file_put_contents( $style_file, $contents );
}
remove_action( 'wp_head', 'wp_custom_css_cb', 101 );
// /* How to Enqueues the external CSS file */
add_action( 'wp_enqueue_scripts', 'my_external_styles' );
function my_external_styles() {
$upload_dir = wp_get_upload_dir();
wp_register_style( 'my-css', sprintf( '%1$s/css', untrailingslashit( $upload_dir['baseurl'] ) ) .'/my.css' );
wp_enqueue_style( 'my-css' );
}