WP_ENQUEUE未打印版本

时间:2017-04-21 作者:Aschab

我正在尝试使用以下行更改插件:

wp_enqueue_script($this->_token . \'-admin\', esc_url($this->assets_url) . \'js/admin\' . $this->script_suffix . \'.js, $scripts, $this->_version);
当我打印$this->\\u版本时,它显示为“3.0.0”,但当管理员。js打印出来

<script type="text/javascript" src="localhost/wp-content/plugins/woocommerce-customer-relationship-manager/assets/js/admin.js"></script>
是否存在任何可能阻止打印版本的挂钩?,或者其他我应该找的环境?

2 个回复
SO网友:Punitkumar Patel

我已经通过插件进行了深入的检查,它是有效的。您的主题可能具有从脚本URL中删除查询字符串的功能。所以请检查你的主题。这是我的实验截图:enter image description here

SO网友:Oleg Butuzov

是的,您可以更改打印方式,检查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();

相关推荐