Skip to content

Instantly share code, notes, and snippets.

@pimpmywp
Last active December 11, 2015 11:18
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pimpmywp/4592649 to your computer and use it in GitHub Desktop.
Save pimpmywp/4592649 to your computer and use it in GitHub Desktop.
[WordPress] wp_enqueue_script(), wp_enqueue_styles() で出力されるタグからtype属性を除いてHTML5ぽくしつつ、scriptタグでも条件付きコメントを使えるようにする
<?php
if ( ! class_exists( 'PM_Scripts' ) ) {
class PM_Scripts Extends WP_Scripts {
public function __construct() {
parent :: __construct();
}
public function print_extra_script( $handle, $echo = true ) {
if ( !$output = $this->get_data( $handle, 'data' ) )
return;
if ( !$echo )
return $output;
echo "<script>\n";
echo "<!--\n";
echo "$output\n";
echo "//-->\n";
echo "</script>\n";
return true;
}
public function do_item( $handle, $group = false ) {
if ( ! isset( $this->registered[$handle]) )
return false;
if ( 0 === $group && $this->groups[$handle] > 0 ) {
$this->in_footer[] = $handle;
return false;
}
$obj = $this->registered[ $handle ];
if ( false === $group && in_array($handle, $this->in_footer, true) )
$this->in_footer = array_diff( $this->in_footer, (array) $handle );
if ( null === $obj->ver )
$ver = '';
else
$ver = $obj->ver ? $obj->ver : $this->default_version;
if ( isset($this->args[$handle]) )
$ver = $ver ? $ver . '&amp;' . $this->args[$handle] : $this->args[$handle];
$src = $obj->src;
if ( $this->do_concat ) {
$srce = apply_filters( 'script_loader_src', $src, $handle );
if ( $this->in_default_dir($srce) ) {
$this->print_code .= $this->print_extra_script( $handle, false );
$this->concat .= "$handle,";
$this->concat_version .= "$handle$ver";
return true;
} else {
$this->ext_handles .= "$handle,";
$this->ext_version .= "$handle$ver";
}
}
$this->print_extra_script( $handle );
if ( !preg_match('|^https?://|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
$src = $this->base_url . $src;
}
if ( !empty($ver) )
$src = add_query_arg('ver', $ver, $src);
$src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) );
$end_cond = $tag = '';
if ( isset( $obj->extra[ 'conditional' ] ) && $obj->extra[ 'conditional' ] ) {
$tag .= "<!--[if {$obj->extra['conditional']}]>\n";
$end_cond = "<![endif]-->\n";
}
$tag .= apply_filters( 'script_loader_tag', "<script src='$src'></script>\n", $handle );
$tag .= $end_cond;
if ( $this->do_concat )
$this->print_html .= $tag;
else
echo $tag;
return true;
}
}
}
<?php
add_filter( 'after_setup_theme', 'my_create_scripts' );
function my_create_scripts() {
require_once( 'class-pm-scripts.php' );
$GLOBALS['wp_scripts'] = new PM_Scripts;
}
add_filter( 'style_loader_tag', 'my_style_loader_tag' );
function my_style_loader_tag( $tag ) {
return preg_replace( array( "| type='.+?'\s*|", "| id='.+?'\s*|", '| />|' ), array( ' ', ' ', '>' ), $tag );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment