0

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.

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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