Skip to content

Instantly share code, notes, and snippets.

@picocodes
Last active February 3, 2021 12:13
Show Gist options
  • Save picocodes/ee360b12c450a6b3c4284dbd1098e0c9 to your computer and use it in GitHub Desktop.
Save picocodes/ee360b12c450a6b3c4284dbd1098e0c9 to your computer and use it in GitHub Desktop.
A gist showing how to conditionally load aui assets
<?php
/**
* Add the function that calls the class.
*/
if(!function_exists('aui')){
function aui(){
global $aui_load_assets;
if(!class_exists("AUI",false)){
return false;
}
$aui_load_assets = true;
return AUI::instance();
}
}
<?php
...
// Plugins can hook here to force aui to load assets on their pages.
if ( apply_filters( 'aui_load_assets', false ) {
/**
* Maybe load CSS
*
* We load super early in case there is a theme version that might change the colors
*/
if ( $this->settings['css'] ) {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ), 1 );
}
if ( $this->settings['css_backend'] && $this->load_admin_scripts() ) {
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_style' ), 1 );
}
} else {
// Plugins did not force scripts to load here.
// Let us load the scripts in case aui() was called.
if ( $this->settings['css'] ) {
add_action( 'wp_footer', array( $this, 'maybe_enqueue_style' ), 1 );
}
if ( $this->settings['css_backend'] && $this->load_admin_scripts() ) {
add_action( 'admin_footer', array( $this, 'maybe_enqueue_style' ), 1 );
}
}
...
/*
* Adds the styles.
*/
public function maybe_enqueue_style() {
global $aui_load_assets;
if ( ! empty( $aui_load_assets ) ) {
$this->enqueue_style();
}
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment