Deregister scripts on unnecessary pages in WP - wordpress

I have installed WP Pokks plugin. I using in on some of pages and need to deregister its styles and scripts on other pages.
I found some code in plugin which registers scripts:
### Function: Enqueue Polls JavaScripts/CSS
add_action('wp_enqueue_scripts', 'poll_scripts');
function poll_scripts() {
// code
}
So for deregistering its scripts i am using next code in my functions.php:
add_action( 'wp_enqueue_scripts', 'deregister_polls_scripts_and_styles' );
function deregister_polls_scripts_and_styles() {
if ( is_home() ) {
remove_action( 'wp_enqueue_scripts', 'poll_scripts');
}
}
So it must deregister polls scripts on homepage, but id does not. What i am doing wrong?

You can use like this
add_action( 'wp_print_scripts', 'de_script', 100 );
function de_script() {
wp_dequeue_script( 'jquery' );
wp_deregister_script( 'jquery' );
}

Related

wp_dequeue_script not work | WordPress 6.1.1

I want to remove custom js and css of core wordpress.
I followed the construction on wordpress.org
function wpdocs_dequeue_script( ) {
wp_dequeue_script( 'jquery-ui-core' );
}
add_action( 'wp_print_scripts', 'wp_dequeue_script', 100 );
Unfortunately, this not work in my case.
I installed WordPress 6.1.1. Is there any changes?
you need to call your function in the hook:
function wpdocs_dequeue_script( ) {
wp_dequeue_script( 'jquery-ui-core' );
}
add_action( 'wp_print_scripts', 'wpdocs_dequeue_script', 100 );

wp_enqueue_style doesn't work but wp_enqueue_scripts works

I have a code like this
function fs_express_theme_styles() {
wp_enqueue_style( 'fs-express-theme-style', get_stylesheet_uri(), array(), filemtime(get_stylesheet_uri()) );
}
add_action( 'wp_enqueue_style', 'fs_express_theme_styles' );
that doesn't work. I tried different versioning and no versioning at all. As a result I don't get any errors in the console or debug files but styles are not loaded.
However, this worked:
function fs_express_theme_scripts() {
wp_enqueue_style( 'fs-express-theme-style', get_stylesheet_uri(), array(), filemtime(get_stylesheet_uri()) );
}
add_action( 'wp_enqueue_scripts', 'fs_express_theme_scripts' );
I am not sure why add_action( 'wp_enqueue_style', 'fs_express_theme_styles' ) didn't work but add_action( 'wp_enqueue_scripts', 'fs_express_theme_scripts' ) worked.
Can someone, please, explain?
At the beggining I tried putting everything in two separate functions like this:
// We define the function:
function MYTHEME_scripts() {
wp_enqueue_script('jquery-ui-datepicker');
}
// Add the functions to WP loading list.
add_action( 'wp_enqueue_scripts', 'MYTHEME_scripts' );
function MYTHEME_styles() {
wp_enqueue_style('jquery-ui-css', '//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css');
wp_enqueue_style('jquery-ui-css', '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css');
}
// Add the functions to WP loading list.
add_action( 'wp_enqueue_style', 'MYTHEME_styles' );
that was taken from here https://wordpress.stackexchange.com/questions/137104/wp-enqueue-script-was-called-incorrectly, but it didn't work so while testing I figured out that loading styles through wp_enqueue_scripts worked
I also added this
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = get_stylesheet_uri();
fwrite($myfile, $txt);
fclose($myfile);
to both functions to test things out and I see that add_action( 'wp_enqueue_style', 'fs_express_theme_styles' ) never worked but add_action( 'wp_enqueue_scripts', 'fs_express_theme_scripts' ) did

How to dequeue or deregister specific woocoomerce script in wordpress admin panel

I need to remove Woocommerce script: wc-admin-order-meta-boxes-js (woocommerce/assets/js/admin/meta-boxes-order.min.js) from the edit-order page or all admin pages in woocommerce. I tried everything but nothing works. Can anyone help me? Thanks.
For example, I tried this:
add_action( 'admin_print_script', 'remove_admin_scripts', 1 );
function remove_admin_scripts() {
// Dequeue
wp_dequeue_script( 'wc-admin-order-meta-boxes-js' );
// Deregister
wp_deregister_script( 'wc-admin-order-meta-boxes-js' );
}
You are calling the wrong name of the script. You can just dequeue as it is enqueued, but with a higher priority.
add_action( 'admin_enqueue_scripts', 'remove_admin_scripts', 99 );
function remove_admin_scripts() {
// Dequeue.
wp_dequeue_script( 'wc-admin-order-meta-boxes' );
// Deregister.
wp_deregister_script( 'wc-admin-order-meta-boxes' );
}
This is tested and works.

-Wordpress plugin- correctly way to include js files without wp_enqueue_scripts

I've been usually to include JavaScript files in my plugins in the following way:
function A_pubblish_scriptFEnd(){
$A_scriptFE=plugins_url( 'Js/CPFront.js', __FILE__ );
wp_enqueue_script( 'CP-js-FE', $A_scriptFE, true );
}
add_action( 'wp_enqueue_scripts', 'A_pubblish_scriptFEnd' );
It worked for every single plugin but now I'm having a problem if I upload more than one plugin WordPress gives me an error, cause I can't repeat wp_enqueue_scripts more than one time.
How can I include in a correct way multiple js files for multiple plugin?
What is the correct way to include files in the main php file?
This is the correct way to enqueue scripts.
/**
* Proper way to enqueue scripts and styles.
*/
function themeslug_enqueue_style() {
wp_enqueue_style( 'my-theme', 'style.css', false );
}
function themeslug_enqueue_script() {
wp_enqueue_script( 'my-js', 'filename.js', false );
}
add_action( 'wp_enqueue_scripts', 'themeslug_enqueue_style' );
add_action( 'wp_enqueue_scripts', 'themeslug_enqueue_script' );
Having multiple actions on the wp_enqueue_scripts hook is perfectly fine.
( https://developer.wordpress.org/reference/hooks/wp_enqueue_scripts/ )

Wordpress Dequeue Script not working (with jquery)

I am trying to dequeue the following plugin scripts:
function afg_enqueue_cbox_scripts() {
wp_enqueue_script('jquery');
wp_enqueue_script('afg_colorbox_script', BASE_URL . "/colorbox/jquery.colorbox-min.js" , array('jquery'));
wp_enqueue_script('afg_colorbox_js', BASE_URL . "/colorbox/mycolorbox.js" , array('jquery'));
}
I tried adding this in functions.php:
add_filter('wp_print_styles', 'remove_mycred', 100);
function remove_mycred() {
wp_dequeue_script( 'afg_colorbox_script' );
wp_dequeue_script( 'afg_colorbox_js' );
}
But it does not work at all - both scripts are still there.
There are other scripts that I have no problems dequeuing - just not those.
I suspect jquery has something to do with my problems?
thanks!
Blaise
You have two problems here, you should use wp_enqueue_scripts hook to hook your function to. Secondly, you will need to go and look at the priority which the author used to enqueue these scripts, and then give your action hook a lower (higher number) priority. Your code should look something like this
function remove_mycred() {
wp_deregister_script( 'afg_colorbox_script' );
wp_dequeue_script( 'afg_colorbox_script' );
wp_deregister_script( 'afg_colorbox_js' );
wp_dequeue_script( 'afg_colorbox_js' );
}
add_action( 'wp_enqueue_scripts', remove_mycred, 9999 );

Resources