admin管理员组

文章数量:1125545

that is my source: view-source:buhehe.de i am trying to deque some script form function.php i mean these two:

<script type="text/javascript" src=".2.1.min.js"></script>
<script type='text/javascript' src='.js?ver=1.12.4'></script>

I added following code in Function.php but it doesn't deques scripts from source:

function dequeue_script() {
   wp_dequeue_script( '.js?ver=4.9.1' );
   wp_dequeue_script( '.min.js?ver=4.9.1' );
      wp_dequeue_script( '.min.js' );
}
add_action( 'wp_print_scripts', 'dequeue_script', 100 ); 

How can i deque these scripts fcrom source?

that is my source: view-source:buhehe.de i am trying to deque some script form function.php i mean these two:

<script type="text/javascript" src="http://buhehe.de/wp-content/themes/tema/js/jquery-3.2.1.min.js"></script>
<script type='text/javascript' src='http://buhehe.de/wp-includes/js/jquery/jquery.js?ver=1.12.4'></script>

I added following code in Function.php but it doesn't deques scripts from source:

function dequeue_script() {
   wp_dequeue_script( 'http://buhehe.de/wp-content/themes/heatt/js/small-menu.js?ver=4.9.1' );
   wp_dequeue_script( 'http://buhehe.de/wp-includes/js/wp-embed.min.js?ver=4.9.1' );
      wp_dequeue_script( 'http://buhehe.de/wp-includes/js/wp-embed.min.js' );
}
add_action( 'wp_print_scripts', 'dequeue_script', 100 ); 

How can i deque these scripts fcrom source?

Share Improve this question edited Jan 1, 2018 at 23:37 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Jan 1, 2018 at 18:57 ნიკა ხაჩიძენიკა ხაჩიძე 891 gold badge3 silver badges7 bronze badges 4
  • 1 someone give you the wrong method in the other question, I add a comment here : wordpress.stackexchange.com/questions/289623/… – mmm Commented Jan 1, 2018 at 19:28
  • How would be correct code? – ნიკა ხაჩიძე Commented Jan 1, 2018 at 20:48
  • Depends on how they're being enqueued. Those script tags could just be hardcoded. – admcfajn Commented Jan 1, 2018 at 21:15
  • One of them is being enqueued not from function.php. how can i remove it? – ნიკა ხაჩიძე Commented Jan 2, 2018 at 8:54
Add a comment  | 

3 Answers 3

Reset to default 1

Try using wp_enqueue_scripts with the handle like this example in your functions.php file :

add_action( 'wp_enqueue_scripts', 'remove_scripts', 100 );
function remove_scripts() {

    wp_dequeue_script( 'original-handle' );
    wp_deregister_script( 'original-handle );

}

However, this will only work if they have been loaded correctly in using wp_enqueue_scripts. Looks like they have been hard coded in a file which isn't best practice in WordPress.

wp_print_scripts hook with set high priority instead of default 10

function dequeue_custom_scripts() {
        // Make sure to use the correct URLs of the scripts you want to dequeue
        $script_url_1 = 'http://buhehe.de/wp-content/themes/tema/js/jquery-3.2.1.min.js';
        $script_url_2 = 'http://buhehe.de/wp-includes/js/jquery/jquery.js?ver=1.12.4';
    
        // Check if the script tags exist in the output buffer
        if (ob_get_contents()) {
            // If the buffer is not empty, clean it to ensure accurate results
            ob_end_clean();
        }
    
        // Get the content of the output buffer
        $html = ob_get_contents();
    
        // Check if the script tags exist in the HTML content
        if (strpos($html, $script_url_1) !== false || strpos($html, $script_url_2) !== false) {
            // Dequeue the scripts by removing them from the HTML content
            $html = str_replace(array($script_url_1, $script_url_2), '', $html);
        }
    
        // Print the modified HTML content
        echo $html;
    }
    
    // Hook the function to wp_print_scripts with a priority greater than the default (10)
    add_action('wp_print_scripts', 'dequeue_custom_scripts', 20);

You could try using the script_loader_tag filter. If they are enqueued properly this should get rid of them based on the URLs you provide:

add_filter('script_loader_tag', 'custom_remove_scripts', 11, 2);
function custom_remove_scripts($link, $handle) {
    $urls = array(
         'http://buhehe.de/wp-content/themes/heatt/js/small-menu.js',
         'http://buhehe.de/wp-includes/js/wp-embed.min.js'
    );
    foreach ($urls as $url) {
        if (strstr($link, $url)) {$link = '';}
    }
    return $link;
}

Otherwise as already stated by others you would need to find where the links are hardcoded in your theme. If you find they are enqueued using wp_enqueue_script then you can remove them using wp_dequeue_script by their script handle not by their URL as in your code.

本文标签: