是的,您可以更改打印方式,检查script_loader_tag
滤器
下面是一个示例,说明了如何使用此技术创建CSS/JS。
<?php
class Hideme {
/**
* Class construct
*
* @return void
*/
function __construct() {
add_action( \'init\', array( $this, \'invisible\' ), 5 );
}
/**
* Class real initator. Setting correct actions and filters.
*
* @return void
*/
function invisible() {
if ( ! is_admin() ) {
// Affcting \'v\' query argument
add_filter( \'script_loader_src\', array( $this, \'loader_src\' ), 20, 2 );
add_filter( \'style_loader_src\', array( $this, \'loader_src\' ), 20, 2 );
// Changing the way how script and link tags are displayed.
add_filter( \'script_loader_tag\', array( $this, \'script_loader_tag\' ), 10, 3 );
add_filter( \'style_loader_tag\', array( $this, \'style_loader_tag\' ), 10, 2 );
}
}
/**
* Tag rebuilder - redo "link" tag with a changed attributes.
*
* @param string $tag Actual "link" tag.
* @param string $handle ID of the resource.
* @return string
*/
function style_loader_tag( $tag, $handle ) {
return $this->tag( \'css\', $tag, $handle );
}
/**
* Tag rebuilder - redo "script" tag with a changed attributes.
*
* @param string $tag Actual "script" tag.
* @param string $handle ID of the resource.
* @param string $src URL of the resources.
* @return string New "script" tag.
*/
function script_loader_tag( $tag, $handle, $src ) {
return $this->tag( \'js\', $tag, $handle );
}
/**
* Tag rebuilder - generate a html tag for script or link with a changed parameters..
*
* @param string $type One of two types can be js or other (for styles).
* @param string $html Actual HTML tag from the previos instance.
* @param string $handle ID of the resource.
* @return string
*/
function tag( $type, $html, $handle ) {
if ( preg_match_all( \'/(\\S+)=\\\'(.*?)\\\'/si\', $html, $m ) ) {
$attr = array();
foreach ( $m[1] as $id => $key ) {
$attr[ $key ] = $m[2][ $id ];
}
$attr[\'id\'] = sprintf( \'%s-%s\', $type, $handle );
foreach ( $attr as $key => $val ) {
$attr[ $key ] = sprintf( \'%s=\\\'%s\\\'\', $key, esc_attr( $val ) );
}
$tag_string_placeholder = \'js\' === $type ? \'<script %s></script>\' : \'<link %s/>\';
$html = sprintf( $tag_string_placeholder, implode( \' \', $attr ) );
}
return $html;
}
/**
* Change the v qury parameter to hashed h parameter.
*
* @param string $src URL of the.
* @param string $handle ID of the resource.
* @return string
*/
function loader_src( $src, $handle ) {
$url_parsed = wp_parse_url( $src );
if ( isset( $url_parsed[\'query\'] ) ) {
parse_str( $url_parsed[\'query\'], $query );
unset( $url_parsed );
if ( isset( $query[\'ver\'] ) ) {
$src = add_query_arg( \'ver\', false, $src );
$src = add_query_arg( \'h\', substr( md5( $src ), 0, 8 ), $src );
}
}
return $src;
}
}
new Hideme();