Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active August 10, 2016 14:11
Show Gist options
  • Save tommcfarlin/5402e0572fb51229ca3321b615a5e534 to your computer and use it in GitHub Desktop.
Save tommcfarlin/5402e0572fb51229ca3321b615a5e534 to your computer and use it in GitHub Desktop.
[WordPress] Loading WordPress Assets, Part 1
<?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() {
}
}
<?php
$this->assets = array(
'css' => array(),
'js' => array(
'acme-app' => array(
'ver' => rand(),
'path' => plugins_url( 'js/app.min.js', __FILE__ ),
'deps' => array( 'jquery' ),
),
),
);
<?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' ),
),
),
);
<?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
);
}
}
<?php
public function enqueue_styles() {
$css = $this->assets['css'];
foreach ( $css as $id => $asset ) {
wp_enqueue_style(
$id,
$asset['path'],
false,
$asset['ver']
);
}
}
<?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