1

I'm trying to alter a plugin with this line:

wp_enqueue_script($this->_token . '-admin', esc_url($this->assets_url) . 'js/admin' . $this->script_suffix . '.js, $scripts, $this->_version);

When I print $this->_version it comes up as '3.0.0' but when the admin.js is printed it comes along as

<script type="text/javascript" src="localhost/wp-content/plugins/woocommerce-customer-relationship-manager/assets/js/admin.js"></script>

Is there any hook that could be preventing the version to be printed?, or any other setting I should be looking for?

3
  • It should work but I have a question that you calling wp_enqueue_script in wp_enqueue_scripts action Am I right? Apr 21, 2017 at 4:57
  • Yes. its all part of the plugin 'woocommerce-customer-relationship-manager' so i think the suppression of the version is more a feature for some reason than an error
    – Aschab
    Apr 21, 2017 at 5:12
  • If you share whole code then it would be easier to help you. Apr 21, 2017 at 9:12

2 Answers 2

0

I have checked deeply through plugin and it's works. Might your theme has some functionality to remove query string from script URL. So please check your theme. Here is my experimented screenshot : enter image description here

0

Yes, you can change the way it's printed, check the script_loader_tag filter.

Here is Example of how you can cahnge CSS/JS using this technique.

<?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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.