Skip to content

Instantly share code, notes, and snippets.

@ideag
Last active March 16, 2018 09:00
Show Gist options
  • Save ideag/448647638d8387d0049bde5e1056a3ab to your computer and use it in GitHub Desktop.
Save ideag/448647638d8387d0049bde5e1056a3ab to your computer and use it in GitHub Desktop.
A simple plugin to add FontAwesome icons via shortcode
<?php
/**
* Plugin Name: FontAwesome
* Version: 0.1.0
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
add_action( 'plugins_loaded', 'FontAwesome' );
function FontAwesome() {
if ( false === FontAwesomeClass::$instance ) {
FontAwesomeClass::$instance = new FontAwesomeClass();
}
return FontAwesomeClass::$instance;
}
class FontAwesomeClass {
public static $instance = false;
public function __construct() {
// Load FontAwesome assets from CDN.
add_action( 'wp_enqueue_scripts', array( $this, 'assets' ) );
// add `defer` attribute to FontAwesome script.
add_filter( 'script_loader_tag', array( $this, 'defer' ), 10, 2 );
// output an icon via [icon] shortcode.
add_shortcode( 'icon', array( $this, 'icon' ) );
}
public function icon( $args = array() ) {
$icon = '<i class="' . $args['name'] . '"></i>';
return $icon;
}
public function defer( $tag, $handle ) {
if ( 'fontawesome' === $handle ) {
$tag = str_replace( ' src', ' defer src', $tag );
}
return $tag;
}
public function assets() {
wp_register_script( 'fontawesome', 'https://use.fontawesome.com/releases/v5.0.8/js/all.js' );
wp_enqueue_script( 'fontawesome' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment