Last active
February 3, 2021 12:13
-
-
Save picocodes/ee360b12c450a6b3c4284dbd1098e0c9 to your computer and use it in GitHub Desktop.
A gist showing how to conditionally load aui assets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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