admin管理员组文章数量:1405998
I tried everything. I even removed all content in functions.php and created a whole new file with just this:
function wpdocs_dequeue_script() {
if (is_singular()) {
wp_dequeue_script( 'gdrts-rating' );
}
}
add_action( 'wp_print_scripts', 'wpdocs_dequeue_script', 100 );
The idea is to remove some Javascript files from my custom post type. I thought I was doing something wrong so I first tried with is_page() and even is_home(), but even that is not working for me. It seems like the code is not fired or so.
This is my plugin:
wp_enqueue_script('gdrts-events', $this->file('js', 'events'), array(), gdrts_settings()->file_version(), false);
wp_enqueue_script('gdrts-rating', $this->file('js', 'rating'), $depend_js, gdrts_settings()->file_version(), true);
So, I also tried to do it like this:
function wpdocs_dequeue_script() {
wp_dequeue_script( 'gdrts-rating' );
}
add_action( 'wp_enqueue_scripts', 'wpdocs_dequeue_script', 100 );
Not working!
I tried everything. I even removed all content in functions.php and created a whole new file with just this:
function wpdocs_dequeue_script() {
if (is_singular()) {
wp_dequeue_script( 'gdrts-rating' );
}
}
add_action( 'wp_print_scripts', 'wpdocs_dequeue_script', 100 );
The idea is to remove some Javascript files from my custom post type. I thought I was doing something wrong so I first tried with is_page() and even is_home(), but even that is not working for me. It seems like the code is not fired or so.
This is my plugin:
wp_enqueue_script('gdrts-events', $this->file('js', 'events'), array(), gdrts_settings()->file_version(), false);
wp_enqueue_script('gdrts-rating', $this->file('js', 'rating'), $depend_js, gdrts_settings()->file_version(), true);
So, I also tried to do it like this:
function wpdocs_dequeue_script() {
wp_dequeue_script( 'gdrts-rating' );
}
add_action( 'wp_enqueue_scripts', 'wpdocs_dequeue_script', 100 );
Not working!
Share Improve this question edited Nov 25, 2019 at 20:48 Siyah asked Nov 25, 2019 at 19:33 SiyahSiyah 1932 silver badges12 bronze badges 9- Can you indent your code? Also why are you trying to deregister a script? Are you sure it's deregistering you want not dequeing? – Tom J Nowell ♦ Commented Nov 25, 2019 at 20:05
- I want to remove it from the DOM... so I don't want it to be loaded on specific pages. – Siyah Commented Nov 25, 2019 at 20:07
- Registering a script doesn't put it in the dom. Enquing it is what puts it in the DOM. You don't want to deregister, you want to dequeue. Also if you use the body class properly then you can make sure your styles don't apply to those pages even if they do get loaded. The same with your scripts – Tom J Nowell ♦ Commented Nov 25, 2019 at 20:10
- Sorry, I don't get what you meant with the body class. You mean for the styling, right? If so, what exactly do you mean? Honestly, my main pain is with scripts (Javascript). Trying to optimise the pages for speed.... but I have the same issue with dequeuing. My code is not working. See also edited question. – Siyah Commented Nov 25, 2019 at 20:27
- you can just take any answer on how to load scripts on on page X and swap the enqueue for a dequeue – Tom J Nowell ♦ Commented Nov 25, 2019 at 20:29
2 Answers
Reset to default 1Please note the below is done via a custom plugin and not in the functions file as most themes when updated will overwrite the functions file.
The below is tested and works to unqueue a script, I have added code that you can use to test if the file is loaded prior to removing it and also I would use your custom post name if you only wish this to be unloaded on that post type in is_singular().
<?php
/*
Plugin Name: Remove_script_Stack_353322
Plugin URI: www.mywebsite
Description: Remove a script when needed
Version: 1.0
Author: Me
Author URI: www.mywebsite
*/
/**
* Enqueue script.
*/
function my_scripts_method() {
//Change your plugin url/name
wp_enqueue_script( 'gdrts-rating', plugins_url( 'test.js' , __FILE__ ));
}
add_action( 'wp_enqueue_scripts', 'my_scripts_method' );
function wpdocs_dequeue_script()
{
// Lets check if gdrts-rating is loaded
$handle = 'gdrts-rating';
$list = 'enqueued';
if (wp_script_is( $handle, $list ))
{
//echo 'Loaded --------------------------------------------------------------------->';
// Custom post type so use the name in -> is_singular('custom_post_name') you can have more post types such as is_singular('custom_post_name', 'custom_post_2')
if (is_singular())
{
// Unload the plugin
wp_dequeue_script( 'gdrts-rating' );
}
}
else
{
// Not loaded do nothing
//echo 'Not loaded --------------------------------------------------------------------->';
}
}
add_action( 'wp_print_scripts', 'wpdocs_dequeue_script', 10 );
Try deregister_script
in addition to dequeue_script
function wpdocs_dequeue_script() {
wp_dequeue_script( 'gdrts-rating' );
wp_deregister_script( 'gdrts-rating' );
}
add_action( 'wp_enqueue_scripts', 'wpdocs_dequeue_script', 100 );
本文标签: phpDeregistering a script in Wordpress seems impossible
版权声明:本文标题:php - Deregistering a script in Wordpress seems impossible 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744966728a2634993.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论