Last active
August 29, 2015 14:15
-
-
Save roborourke/77b137362171b238efc1 to your computer and use it in GitHub Desktop.
Enqueue HTML snippets using WP Dependencies API
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_action( 'init', array( 'Enqueue_HTML', 'get_instance' ) ); | |
class Enqueue_HTML { | |
public $scripts = array(); | |
protected static $instance; | |
public static function get_instance() { | |
if ( self::$instance === null ) { | |
self::$instance = new self; | |
} | |
return self::$instance; | |
} | |
public function __construct() { | |
add_filter( 'script_loader_src', array( $this, 'loader_src' ), 9, 2 ); | |
add_filter( 'script_loader_tag', array( $this, 'loader_tag' ), 9, 3 ); | |
} | |
public function loader_src( $src, $handle ) { | |
$str_src = preg_replace( '/^[^<]*(<script.*?<\/script>).*$/sm', '$1', $src ); | |
if ( strpos( $str_src, '<script' ) === 0 ) { | |
$this->scripts[ $handle ] = $str_src; | |
} | |
return $src; | |
} | |
public function loader_tag( $html, $handle, $src ) { | |
if ( isset( $this->scripts[ $handle ] ) ) { | |
return $this->scripts[ $handle ]; | |
} | |
return $html; | |
} | |
} |
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_action( 'admin_enqueue_scripts', function() { | |
// backbone template | |
wp_enqueue_script( 'html', ' | |
<script type="text/html" id="example-backbone"> | |
<div class="my-template" style="left: 0;"> | |
<h1>{{ data.title }}</h1> | |
</div> | |
</script> | |
', array( 'jquery' ) ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment