javascript - Removing auto versioning of JS and loading to header

admin2025-06-03  5

I want to remove the JS I added and gets versioned by wp. I have a js script called base.js, and wordpress loads that by itself like

<script type="text/javascript" src="../base.js?ver=4.9.9"></script>

this code removes the version number

function remove_cssjs_ver( $src ) {
    if( strpos( $src, '?ver=' ) )
        $src = remove_query_arg( 'ver', $src );
    return $src;
}
add_filter( 'style_loader_src', 'remove_cssjs_ver', 1000 );
add_filter( 'script_loader_src', 'remove_cssjs_ver', 1000 );

But what I want to do is stop WP from loading this JS completely. i tried deregestering and dequeueing but it didnt work.

I want to remove the JS I added and gets versioned by wp. I have a js script called base.js, and wordpress loads that by itself like

<script type="text/javascript" src="../base.js?ver=4.9.9"></script>

this code removes the version number

function remove_cssjs_ver( $src ) {
    if( strpos( $src, '?ver=' ) )
        $src = remove_query_arg( 'ver', $src );
    return $src;
}
add_filter( 'style_loader_src', 'remove_cssjs_ver', 1000 );
add_filter( 'script_loader_src', 'remove_cssjs_ver', 1000 );

But what I want to do is stop WP from loading this JS completely. i tried deregestering and dequeueing but it didnt work.

Share Improve this question asked Feb 17, 2019 at 4:18 kydroninkydronin 212 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

If I'm reading correctly you just want to deregister a script but failed... I think that you tried to deregister it too early( before was added ).

add_action( 'wp_print_scripts', 'example' );
function example() {
   wp_dequeue_script( 'name_of_script' );
}

By calling the function in 'wp_print_scripts' should be fine, which is latter than 'wp_enqueue_scripts', where you usually add.

If you want it to stop loading the javascript file then just remove the PHP code you have adding that script.

What it really sounds like is that you don't want it to load on specific pages. You can use Brada's answer to dequeue a script (which I have to do for my plugins ALL time time due to sloppy developers), but really you should make sure that you're correctly registering and enqueueing the script, only when needed.

You should first register the script using the wp_enqueue_scripts:

function wpdocs_theme_name_scripts() {
    wp_register_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'wpdocs_theme_name_scripts' );

And then and ONLY WHEN needed should you be calling wp_enqueue_script. This is one of the biggest issues I see with WordPress developers --- enqueuing scripts and styles on EVERY single page load -- which is BAD WordPress development practices.

You can then normally call wp_enqueue_script or wp_enqueue_style in PHP code or files that is ran on that specific page you need it used on.

The other option is to check the page in the wp_enqueue_scripts action to make sure it's your page before calling enqueue

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748897295a314634.html

最新回复(0)