I am using WordPress and I installed the Woocommerce plugin.
I added the template folder in my theme and started to customize my theme.
Now, I have to remove the Woocommerce.css from my theme and I found code on the official website here
I added the same code in the function.php
add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
or
add_filter( 'woocommerce_enqueue_styles', '__return_false' );
but both answers are not working. I have installed the 5.7.1 Woocommerce.
How can I solve this issue?
function.php
function customtheme_scripts() {
wp_enqueue_style( 'customtheme-style', get_stylesheet_uri(), array(), time() );
wp_style_add_data( 'customtheme-style', 'rtl', 'replace' );
wp_enqueue_script( 'customtheme-navigation', get_template_directory_uri() . '/js/navigation.js', array(), _S_VERSION, true );
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
wp_dequeue_style( 'customtheme-woocommerce-style' );
}
add_action( 'wp_enqueue_scripts', 'customtheme_scripts' );
view source
The domain name is just an example.
This answer has been fully tested on woocommerce 5.x+ and works fine on the default woo style sheets! If you're using a custom theme and/or custom style sheet then you may encounter some discrepancies.
What you see on the documentation page, no longer works on woo 4+, according to their github page.
So you need to dequeue its styles!
wp_dequeue_styleDocs
So if you only want to remove woocommerce.css file, then you could do this:
add_action('wp_enqueue_scripts', 'removing_woo_styles');
function removing_woo_styles()
{
wp_dequeue_style('woocommerce-general'); // This is "woocommerce.css" file
}
However, if you want to remove all of the style sheets loaded by woo, then you could use this:
add_action('wp_enqueue_scripts', 'removing_woo_styles');
function removing_woo_styles()
{
wp_dequeue_style('wc-block-vendors-style');
wp_dequeue_style('wc-block-style');
wp_dequeue_style('woocommerce-general');
wp_dequeue_style('woocommerce-layout');
wp_dequeue_style('woocommerce-smallscreen');
}
If you still can see the styles, then try to clean your cache.
UPDATE related to the question "custom style sheet"
At the time that I was writing the answer you had not provided any screenshot of your style sheet, nor did you say anything about using a custom style sheet. That's why you were not able to get it to work.
Please do NOT copy/paste if you're using a custom style sheet, like the custom css file used in the question. wp_dequeue_style function takes your style sheet handle as the argument. So please read the documentation first. You're using a custom handle name (i.e "customtheme-woocommerce-style"), therefore, you need to use that handle name.
add_action('wp_enqueue_scripts', 'removing_woo_styles');
function removing_woo_styles()
{
wp_dequeue_style('customtheme-woocommerce-style'); // This is your "custom style sheet" file.
}
Also note that commenting out the enqueue section in the main file (i.e inc/woocommerce.php) may work temporarily but on the next woo update, it'll come back again. So, it's recommended to avoid updating your template files as much as possible unless you really have to which is not the case here!
Related
I used a plugin to auto-generate a child theme and necessary files, and it did a good job...my new style.css loads after the parent and works great. the problem is that I have a plugin that requires heavy modification to it's injected divs, and it is a nightmare to overwrite anything because they !important the entire sheet, so I have to work around it.
I'm trying to enqueue my child-theme style.css after the plugin using a priority 99 thing I found on another thread here, and it isn't doing anything. All I did was append to the end of my child functions.php.
Potential issue 1: I did not remove the previous code to enquee this css file...do I need to do that? I thought maybe it would just re-enqueue it if it got to this new code at the end of the file. But maybe it does it once and ignores my code since it's already done.
Potential issue 2: Code error on my part. If my style.css is in the same folder as my functions.php I refer to the file as just 'style.css' right? I assume so because of relative locations because I was not sure if PHP required something that I am unaware of.
Here is my code that is not working.
wp_enqueue_style( 'child-css', get_template_directory_uri() . 'style.css' );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles', 99 );
I also saw this in my functions.php...it's weird because it looks like it's trying to enqueue a parent theme css file, but the relative location points to the child theme. Very confusing. it already had the same add_action priority thing in it from the thing I posted above, so I just change the number to 99, but it also didn't work. It was set to 10.
add_filter( 'locale_stylesheet_uri', 'chld_thm_cfg_locale_css' );
if ( !function_exists( 'chld_thm_cfg_parent_css' ) ):
function chld_thm_cfg_parent_css() {
wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit( get_template_directory_uri() ) . 'style.css', array( 'font-awesome-5-free','saasland-dark-support','bootstrap','themify-icon','saasland-elementor','saasland-remove-animation','magnific-popup','eleganticons','saasland-wpd','saasland-main','saasland-elements','saasland-comments','saasland-footer','saasland-gutenberg' ) );
}
endif;
add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css', 99 );
Any tips? Thanks
get_template_directory_uri will always refer to the parent theme folder for assets.
get_stylesheet_directory_uri will refer to the "current" theme folder for assets (which could be the parent or the child, depending on where it is called).
If you are enqueueing style.css file for the child theme then,
wp_enqueue_style( 'child-css', get_stylesheet_directory_uri() . '/style.css' );
I use Elementor to build my website and there are a lot of functionalities that I'm not using but are none the less loaded on every page of my website. So I decided to dequeue the css files I'm not using in my child theme's functions.php and dequeue the css files which I'm only partially using, replacing them with a 'cleaned-up' version of the file.
This is how I wanted to start doing it:
function adg_dequeue_unnecessary_files() {
wp_dequeue_style( 'elementor-frontend' ); // remove Elementor's custom-frontend.min.css
wp_deregister_style( 'elementor-frontend' );
wp_register_style( 'new-elementor-frontend-css', get_stylesheet_directory_uri() . '/custom-frontend.min.css' ); // Purified replacement for Elementor's custom-frontend.min.css
wp_enqueue_style( 'new-elementor-frontend-css' );
}
add_action( 'wp_enqueue_scripts', 'adg_dequeue_unnecessary_files' );
But while the second part of my function adds my new custom css file nicely, the first part removes almost 10 other Elementor's css files along with the one I actually wanted to dequeue.
This is the list of files being dequeued:
custom-frontend.min.css
post-1501.css (this is the css file of the page I was looking at while making these changes)
frontend-legacy.min.css
post-1396.css (some global Elementor's css)
post-3556.css (this one and the 5 below are templates from a plugin I'm using across my website)
post-4473.css
post-5653.css
post-3489.css
post-3464.css
post-3458.css
I'm guessing it has something to do with the handler 'elementor-frontend' not being correct. The custom-frontend.min.css file had the 'elementor-frontend-css' ID in the link tag of the HTML code, so I was guessing the handler from there.
Does anyone know how I can dequeue only the custom-frontend.min.css file?
After that I wanted to dequeue these files as well:
animations.min.css
elementor-icons.min.css
global.css
frontend-legacy.min.css
swiper.min.js
I've been browsing this for a few days and I'm starting to feel lost, so any help will be much appreciated!
You can dequeue the Elementor CSS file with the use of wp_deregister_style and wp_dequeue_style. For this, you need to pass the CSS file handle name. You can use the below code to dequeue the Elementor plugin global.css file.
function dequeue_elementor_global__css() {
wp_dequeue_style('elementor-global');
wp_deregister_style('elementor-global');
}
add_action('wp_print_styles', 'dequeue_elementor_global__css', 9999);
Here elementor-global is the handle name of the global.css file. You can get any file handle name by stylesheet id. For example:
If any stylesheet id is the elementor-global-css then this file handle will be elementor-global
My understanding is that all Elementor frontend styles, e.g. your post-1234.css files, are children of 'elementor-frontend', which means if you unload it, none of them will load.
If you load your new, optimised frontend.min.css files with the same name, then it should work.
e.g.
function adg_dequeue_unnecessary_files() {
wp_dequeue_style( 'elementor-frontend' ); // remove Elementor's custom-frontend.min.css
wp_deregister_style( 'elementor-frontend' );
wp_register_style( 'elementor-frontend', get_stylesheet_directory_uri() . '/custom-frontend.min.css' ); // Purified replacement for Elementor's custom-frontend.min.css
wp_enqueue_style( 'elementor-frontend' );
}
add_action( 'wp_enqueue_scripts', 'adg_dequeue_unnecessary_files' );
Also. Can you not just add your custom-frontend.min.css to the relevant location in your Child Theme and it will overwrite the Parent theme version by default?
This seems to work. Tested on a few pages and posts:
add_action( 'elementor/frontend/after_enqueue_styles', function() {
wp_deregister_style( 'elementor-frontend' );
wp_dequeue_style( 'elementor-frontend' );
wp_register_style( 'elementor-frontend', get_stylesheet_directory_uri() . '/assets/css/custom-elementor-front-end.css' );
wp_enqueue_style( 'elementor-frontend', get_stylesheet_directory_uri() . '/assets/css/custom-elementor-front-end.css' );
} );
New to wordpress so struggling a little bit here.
Basically I've got 4 custom page templates that I need to create: Home, Portfolio, About, Contact.
For each of these new pages I need a new CSS file. I've enqueued my script portfolio.css as such:
function my_custom_styles() {
wp_register_style( 'portfolio',
get_template_directory_uri().'/portfolio.css' );
if ( is_portfolio ) {
wp_enqueue_style( 'portfolio' );
}
}
add_action( 'wp_enqueue_scripts', 'my_custom_styles' );
This works great - my only problem is that my main stylesheet style.css is clashing with this and overwriting a lot of the styles. Basically both style.css and portfolio.css are loading at the same time.
How do I get it so that only style.css loads on my Homepage, portfolio.css loads on my Portfolio page, about.css only loads on my about page etc.
I can't seem to find any help online for this!
Cheers guys!
Ideally, portfolio.css, about.css, etc. would extend style.css, where most of the core styles (font size, weight, padding, accent declarations, etc.) would be defined in style, but portfolio would add some additional styles not used elsewhere.
If you truly want them to be mutually exclusive, you'll have to dequeue the style.css file.
View your page source and search for /YOURACTIVETHEME/style.css. Note the id="some-style-id" in the <link> tag. That's the "handle" for the stylesheet, and you can use wp_deregister_style() or wp_dequeue_style() to remove it.
Also, clean up your code, it's going to get hard to maintain with those inconsistent indentations. Going through your code, are you sure your if statement is working? is_portfolio is being as a constant right now… You're probably intending it to be passed using something like the is_page() or is_page_template() functions.
add_action( 'wp_enqueue_scripts', 'my_custom_styles' );
function my_custom_styles() {
wp_register_style( 'portfolio', get_template_directory_uri().'/portfolio.css' );
if( is_page('portfolio') ){ // Use Page Title, Page Slug, or Page ID here
wp_enqueue_style( 'portfolio' );
wp_dequeue_style( 'YOUR-ID-FROM-EARLIER' );
}
}
All of this said, if you have custom page templates, you could just use put
wp_enqueue_style( 'portfolio', get_template_directory_uri().'/portfolio.css' );
wp_dequeue_style( 'YOUR-ID-FROM-EARLIER' );
in the page template files.
Another thing to consider is that you're using get_template_directory_uri() which will pull from the parent theme. If you want the active child theme that uses a template (ala Genesis) you'll need to use get_stylesheet_directory_uri() instead.
I am trying to link my stylesheet to another page in Wordpress. The actual Wordpress installation is within a folder, within the actual site. It's set up this way because I only want to use WP for a specific section of the site (it was an afterthought, I know this is isn't necessarily the "correct" way to do things...)
I have the front page set up and the styles are all working fine. But when a create a new page and try to use get_header to pull in the styles, they don't work. The browser is looking for a page called styles.css, not a stylesheet.
I've tried to use "enqueue" in the functions.php file, but it still won't work. I have a copy of my style sheet in the theme folder and also one inside a css folder.
Example of using enqueue for the copy inside the css folder:
wp_enqueue_script( 'styles', 'get_stylesheet_directory_uri()' . 'css/styles2.css' );
*I am using get_header in my page template file, (same header as the front page which is working fine), and it is linked this way:
<link rel="stylesheet" type="text/css" href="../css/styles2.css">
I'm pretty sure the issue is the "../" but when I substitute echo get_stylesheet_directory_uri()....... instead of the ../, it doesn't work as it should.
Any help would be great as I'm newer to WP development.
Thanks everyone
You have to write like this for linking template style sheet ...
wp_enqueue_script( 'styles', get_template_directory_uri(). 'css/styles2.css', array(), '0.0.1' );
Add Style sheet like this:
wp_enqueue_style( 'styles', bloginfo('template_url').'/css/styles2.css' );
You can view more detail at here
You need to hook the css:
If you are using child theme then hook like:
add_action( 'wp_enqueue_scripts', 'enqueue_unique_function_name_here', 0);
function enqueue_unique_function_name_here()
{
wp_enqueue_style( 'css_unique_handle_name_here', get_template_directory_uri(). 'folder_path_inside_child_theme/style_sheet_file_name_here.css', array(), '0.0.1' );
}
If you are using parent theme (no child theme) then hook like:
add_action( 'wp_enqueue_scripts', 'enqueue_unique_function_name_here', 0);
function enqueue_unique_function_name_here()
{
wp_enqueue_style( 'css_unique_handle_name_here', get_stylesheet_directory_uri(). 'folder_path_inside_child_theme/style_sheet_file_name_here.css', array(), '0.0.1' );
}
If want to enqueue in admin side then just change hook name "wp_enqueue_scripts" to "admin_enqueue_scripts".
Try now.
You have used wp_enqueue_script() instead of wp_enqueue_style()
wp_enqueue_style used for Enqueue Style
wp_enqueue_script used for Enqueue Script
wp_enqueue_style( 'styles', 'get_stylesheet_directory_uri()' . 'css/styles2.css' );
Here is the full example for the same.
add_action( 'wp_enqueue_scripts', 'enqueue_custom_style');
function enqueue_custom_style()
{
wp_enqueue_style( 'styles', 'get_stylesheet_directory_uri()' . 'css/styles2.css' );
}
I need to put a styled map on a contact page in WP.
I'd rather not use a plugin as it would be overkill, embedding on the other way won't allow me to customize layers, use placeholders, etc
I coded an example map on a static html page. https://dl.dropboxusercontent.com/u/13823768/map/test.html
How do I get from here to wordpress?
EDIT: I'm working with a child theme so I put this in functions.php (in my child-theme dir)
function enqueue_custom_scripts() {
wp_enqueue_script('google-map-api','https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&language=it');
wp_enqueue_script('google-map-style', get_stylesheet_directory_uri() . '/js/map.js', array(), '1.0.0', true );
}
add_action('wp_enqueue_scripts', 'enqueue_custom_scripts');
?>
Looks ok so far but it needs <body onload="initialize()"> too.
Do you know how I can add the onload to the body tag?
You can accomplish this by creating a custom theme page template: http://codex.wordpress.org/Page_Templates or simply add the #map_canvas element in the "text" view of the WSYWYG content area of the page editor.
The next step is to add all of your map scripts, I would do this by enqueuing the scripts in your theme's functions.php by creating a callback function that is called on the wp_enqueue_scripts action: http://codex.wordpress.org/Function_Reference/wp_enqueue_script.
the function you will add to functions.php would look something like this(replacing the filepaths with your scripts):
/**
* Proper way to enqueue scripts and styles
*/
function theme_name_scripts() {
wp_enqueue_style( 'style-name', get_stylesheet_uri() );
wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
I know you mentioned not wanting a plugin for overkill, but a plugin would allow portability of your code and would allow you to switch themes without losing your map code. Adding a plugin and shortcode to render the #map_canvas element would not be much more time than adding the code to functions.php. If your interested in writing a custom plugin, http://codex.wordpress.org/Writing_a_Plugin