disable a stylesheet in certain wordpress page - css

i have stylesheet that loads on all of my pages in my wordpress theme. i want to disable it in landing.php .
The file is in this address: http://example.com/wp-content/uploads/fusion-styles/fusion-22528.css
i had tried these codes so far:
function themeslug_enqueue_style() {
if (is_page_template( 'landing.php' )) {
wp_enqueue_style( 'stylesheet', 'http://example.com/wp-
content/uploads/fusion-styles/fusion-22528.css', false );
}
}
add_action( 'wp_enqueue_scripts', 'themeslug_enqueue_style' );
and this one:
function se_remove_styles() {
if ( is_page_template( 'landing.php' ) ) {
wp_dequeue_style( 'http://example.com/wp-content/uploads/fusion-
styles/fusion-22528.css?timestamp=1510985246&ver=4.9' );
}
}
add_action( 'wp_print_styles', 'se_remove_styles', 99999 );action(
'wp_print_styles', 'se_remove_all_styles', 999999 );
but didnt worked. i had use this code:
function se_remove_all_styles() {
global $wp_styles;
if ( is_page_template( 'landing.php' ) ) {
$wp_styles->queue = array();
}
}
add_action( 'wp_print_styles', 'se_remove_all_styles', 99 );
but it will remove all of my styles.
any help will appreciated.

This code snippet removes queued CSS called "animate" from your WP frontend.
function se_remove_styles() {
wp_dequeue_style( 'animate' );
}
add_action( 'wp_enqueue_scripts', 'se_remove_styles',99999 );
So, how to get handle name of queued CSS? There are some ways to do it, here is one of them:
Go to view-source:yourwebsite.com
CTRL+F and find your needed CSS file.
You will see the line like this:
< link rel="stylesheet" id="fusionblabla-css" href="http://example.com/wp-content/uploads/fusion-styles/fusion-22528.css" type="text/css" media="all" />
You see id='fusionblabla-css' there. Then "fusionblabla" is the handle name you are looking for. (not 'fusionblabla-css', but "'fusionblabla')
After it is done and you make sure that it is working you can add condition to that function
function se_remove_styles() {
if (SOME CONDITION HERE)
wp_dequeue_style( 'animate' );
}
add_action( 'wp_enqueue_scripts', 'se_remove_styles',99999 );

Related

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

Deque Animation CSS

I am trying to deque the animation library that came with elementor from loading on a particular page. This is the URL of the script
https://wpcalculators.com.ng/wp-content/plugins/elementor/assets/lib/animations/animations.min.css?ver=3.0.16
I have used this code:
//Remove animation
function remove_animation() {
if(is_page([19] )):
wp_dequeue_style( 'elementor-animations' );
endif;
}
add_action( 'wp_enqueue_scripts', 'remove_animation', 100 );
but it is not working.
I used similar code to deque Gutenberg block library from loading and it worked.
//Remove Gutenberg Block Library CSS from loading on the frontend
function smartwp_remove_wp_block_library_css() {
if(is_page([19] )):
wp_dequeue_style( 'wp-block-library' );
wp_dequeue_style( 'wp-block-library-theme' );
endif;
}
add_action( 'wp_enqueue_scripts', 'smartwp_remove_wp_block_library_css' );
I confirmed the ID of the CSS from the source code which reads:
The Id seems to be 'elementor-animations'
How else can I write the code to make it work?
Note: I know it's not working when I test the URL with pagespeed insight.
Probably, deregister styles before dequeuing could solve the problem
function remove_animation() {
if ( is_page( [ 19 ] ) ):
wp_deregister_style( 'elementor-animations' );
wp_dequeue_style( 'elementor-animations' );
endif;
}
add_action( 'wp_enqueue_scripts', 'remove_animation', 100 );

Dequeue scripts and styles on login page

I am trying to find a way to dequeue styles and scripts on the "login, password reset and register " default wp pages.
I understand there is 'login_enqueue_scripts' but no such thing as login_dequeue_scripts.
Whats your approach ? Ive tried something like this:
add_filter('body_class', function($classes) {
if (in_array('login', $classes)) {
wp_dequeue_style( 'list-css' );
wp_dequeue_style( 'blog-css' );
wp_dequeue_style( 'dir-css' );
wp_dequeue_style( 'author-css' );
}
return $classes;
});```
You could just use the wp_dequeue_script/style function for the login_enqueue_script action. Like so:
function custom_login_page() {
wp_dequeue_style( 'list-css');
}
add_action( 'login_enqueue_scripts', 'custom_login_page' );
The action of 'login_enqueue_scripts' is what happens when the other scripts/styles are loaded on that page. You can create a custom function to run when that happens, which is what you want.
However, rather than dequeueing, why not just write some of your own CSS for the elements on the page, then enqueue whatever that custom CSS?
function custom_login_css() {
wp_enqueue_style( 'custom-login-styles', get_stylesheet_directory_uri() . '/custom-login.css' );
}
add_action( 'login_enqueue_scripts', 'custom_login_css' );
Here's a good reference for the different elements you can customize with CSS on the WP login page.

wp_enqueue_style is not working

I have a page that I want to apply custom css file, but I am having difficulty loading the custom css file for my page 'homepage'. If anyone could help me out, it would be highly appreciated. thank you!
This part works:
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css');
}
this part does not :(
function addcssAndScripts()
{
if ( is_page( 'homepage' ) )
{
wp_enqueue_style( '/stylehome.css', get_stylesheet_uri() );
}
}
add_action( 'wp_enqueue_scripts', 'addcssAndScripts');
You're misunderstanding how wp_enqueue_style should be used.
In the example below the first argument I pass in is a handle, 'style-home'. The second argument is the path to the file.
function wpse_load_scripts_styles() {
if ( is_page( 'homepage' ) ) {
wp_enqueue_style( 'style-home', get_stylesheet_directory_uri() . '/stylehome.css' );
}
}
add_action( 'wp_enqueue_scripts', 'wpse_load_scripts_styles' );
get_stylesheet_directory_uri() gets the URL to your child theme folder. get_template_directory_uri() which is used in the first block of code in your question gets the path to the parent theme directory. Select the correct function based on where the file is located.
My guess would be that you've set the page, homepage, as your front page therefore you may want to replace the conditional with is_front_page().
Replace:
if ( is_page( 'homepage' ) ) {
With:
if ( is_front_page() ) {
Finally get_stylesheet_uri() as used in the second block of code in your question will return the URL of the child theme's stylesheet (or the parent theme if you don't have a child theme setup).
Further reading: http://codex.wordpress.org/Function_Reference/wp_enqueue_style
Will this work for you?
function addcssAndScripts()
{
if ( is_page( 'homepage' ) )
{
wp_enqueue_style( 'child-home-style', get_stylesheet_directory_uri . '/stylehome.css');
}
}
add_action( 'wp_enqueue_scripts', 'addcssAndScripts');
http://codex.wordpress.org/Function_Reference/get_stylesheet_directory_uri
http://codex.wordpress.org/Function_Reference/wp_enqueue_style

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