-
-
Save tommcfarlin/5402e0572fb51229ca3321b615a5e534 to your computer and use it in GitHub Desktop.
[WordPress] Loading WordPress Assets, Part 1
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 | |
namespace Acme\Admin; | |
class Assets_Loader { | |
private $assets; | |
public function __construct() { | |
$plugin_root = trailingslashit( dirname( dirname( __FILE__ ) ) ); | |
$this->assets = array( | |
'css' => array(), | |
'js' => array(), | |
), | |
); | |
} | |
public function init() { | |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) ); | |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); | |
} | |
public function enqueue_styles() { | |
} | |
public function enqueue_scripts() { | |
} | |
} |
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 | |
$this->assets = array( | |
'css' => array(), | |
'js' => array( | |
'acme-app' => array( | |
'ver' => rand(), | |
'path' => plugins_url( 'js/app.min.js', __FILE__ ), | |
'deps' => array( 'jquery' ), | |
), | |
), | |
); |
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 | |
$this->assets = array( | |
'css' => array( | |
'acme-admin' => array( | |
'ver' => rand(), | |
'path' => plugins_url( 'css/admin.css', __FILE__ ), | |
), | |
), | |
'js' => array( | |
'acme-app' => array( | |
'ver' => rand(), | |
'path' => plugins_url( 'js/app.min.js', __FILE__ ), | |
'deps' => array( 'jquery' ), | |
), | |
), | |
); |
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 | |
public function enqueue_scripts() { | |
$js = $this->assets['js']; | |
foreach ( $js as $id => $asset ) { | |
wp_enqueue_script( | |
$id, | |
$asset['path'], | |
$asset['deps'], | |
$asset['ver'], | |
false | |
); | |
} | |
} |
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 | |
public function enqueue_styles() { | |
$css = $this->assets['css']; | |
foreach ( $css as $id => $asset ) { | |
wp_enqueue_style( | |
$id, | |
$asset['path'], | |
false, | |
$asset['ver'] | |
); | |
} | |
} |
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 | |
namespace Acme\Admin; | |
class Assets_Loader { | |
private $assets; | |
public function __construct() { | |
$plugin_root = trailingslashit( dirname( dirname( __FILE__ ) ) ); | |
$this->assets = array( | |
'css' => array( | |
'acme-admin' => array( | |
'ver' => rand(), | |
'path' => plugins_url( 'css/admin.css', __FILE__ ), | |
), | |
), | |
'js' => array( | |
'acme-app' => array( | |
'ver' => rand(), | |
'path' => plugins_url( 'js/app.min.js', __FILE__ ), | |
'deps' => array( 'jquery' ), | |
), | |
), | |
); | |
} | |
public function init() { | |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) ); | |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); | |
} | |
public function enqueue_styles() { | |
$css = $this->assets['css']; | |
foreach ( $css as $id => $asset ) { | |
wp_enqueue_style( | |
$id, | |
$asset['path'], | |
false, | |
$asset['ver'] | |
); | |
} | |
} | |
public function enqueue_scripts() { | |
$js = $this->assets['js']; | |
foreach ( $js as $id => $asset ) { | |
wp_enqueue_script( | |
$id, | |
$asset['path'], | |
$asset['deps'], | |
$asset['ver'], | |
false | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment