Skip to content

Instantly share code, notes, and snippets.

@jhned
Last active April 20, 2020 19:24
Show Gist options
  • Save jhned/caac3730da4495ef9f9a57f234dfb825 to your computer and use it in GitHub Desktop.
Save jhned/caac3730da4495ef9f9a57f234dfb825 to your computer and use it in GitHub Desktop.
WordPress Enqueues Class using prefetch
<?php
/**
* Class Enqueues
*/
class Enqueues {
/**
* Used for version-ing of enqueued assets
*
* @var string
*/
private $version;
/**
* Enqueues constructor.
*/
public function __construct() {
$this->version = NAMESPACED_THEME_VERSION;
add_action( 'init', array( $this, 'conditional_hooks' ) );
add_action('wp_enqueue_scripts', array($this, 'register_theme_assets'), 20);
add_action('wp_enqueue_scripts', array($this, 'enqueue_theme_assets'), 50);
}
/**
* WordPress conditional hooks
*/
public function conditional_hooks() {
// This conditional defines the views that should use the prefetch workflow. Other views load scripts and styles as normal.
if ( is_front_page() ) {
add_filter( 'script_loader_tag', array( $this, 'filter_script_tag' ), 10, 3 );
add_filter( 'style_loader_tag', array( $this, 'filter_style_tag' ), 10, 4 );
}
}
/**
* Register theme stylesheets and scripts
*/
public function register_theme_assets() {
$min = '';
if (!defined('SCRIPT_DEBUG') || false === SCRIPT_DEBUG) {
$min = '.min';
}
wp_register_script('site-script', get_template_directory_uri() . '/dist/js/site' . $min . '.js', array(), $this->version, 'true');
wp_register_style('site-style', get_template_directory_uri() . '/dist/css/style.css', array('site-typekit'), $this->version, 'all');
// View-specific stylesheets (pair up with enqueues below)
wp_register_style('site-style-home-no-defer', get_template_directory_uri() . '/dist/css/style.home.css', array(), $this->version, 'screen');
}
/**
* Enqueue registered assets
*/
public function enqueue_theme_assets() {
if ( is_front_page() ) {
wp_enqueue_style('site-style-home-no-prefetch');
}
wp_enqueue_style('site-style');
wp_enqueue_script('site-script');
}
/**
* filter_script_tag
*
* Defer all scripts
*
* @param string $tag The `<script>` tag for the enqueued script.
* @param string $handle The script's registered handle.
* @param string $src The script's source URL.
*
* @return string The script tag.
*/
public function filter_script_tag($tag, $handle, $src) {
return sprintf('<%2$s src="%1$s" defer></%2$s>' . "\n", $src, 'script');
}
/**
* filter_style_tag
*
* Prefetch any stylesheet not set to "no-prefetch" in the handle.
*
* @param string $tag The `<link>` tag for the enqueued script.
* @param string $handle The script's registered handle.
* @param string $href The style's source URL.
* @param string $media The style's media.
*
* @return string The script tag.
*/
public function filter_style_tag($tag, $handle, $href, $media) {
if (false === strpos($handle, 'no-prefetch')) {
return '<link id="' . $handle . '-css" rel="prefetch" href="' . $href . '" as="style" onload="this.onload=null;this.rel=\'stylesheet\'" media="' . $media . '"><noscript><link rel="stylesheet" href="' . $href . '"></noscript>';
}
return $tag;
}
}
new Enqueues();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment