I'm trying to get rid of unused styles in my WP. I'm doing it like this:
// remove CSS
add_action('wp_dequeue_style', 'remove_css');
function remove_css() {
// boostrap shortcodes
wp_dequeue_style(array(
'bs_bootstrap-css',
'bs_shortcodes-css'
));
// woocommerce
wp_dequeue_style('woocommerce-layout-css');
wp_dequeue_style('woocommerce-smallscreen-css');
wp_dequeue_style('woocommerce-general-css');
}
Problem is, neither first nor the secound example works. I tried using wp_deregister_script and setting a priority to add_action. What can I do?
You should hook into wp_enqueue_scripts:
add_action( 'wp_enqueue_scripts', 'so26892641_remove_css', 25 );
function so26892641_remove_css(){
wp_dequeue_style('woocommerce-layout');
// etc
}
Woocommerce has a filter for that, also note that the handles do not have "-css" at the end.
add_filter( 'woocommerce_enqueue_styles', 'jk_dequeue_styles' );
function jk_dequeue_styles( $enqueue_styles ) {
unset( $enqueue_styles['woocommerce-general'] ); // Remove the gloss
unset( $enqueue_styles['woocommerce-layout'] ); // Remove the layout
unset( $enqueue_styles['woocommerce-smallscreen'] ); // Remove the smallscreen optimisation
return $enqueue_styles;
}
Code shamelessly copied from the following link :)
Related
I am trying to edit the styles of the Woocommerce checkout page that is coming from style.dev.css. I tried to copy the file to my child theme directory and made the changes in the new child file. However, it seems that the page is still loading the original file and not the theme file The reason I am not doing my edits in the style.css child file is that most selectors have !important and don't want to do heavy overwriting. Does anyone know what I am missing, please?
The documentation of WooCommerce could help you acheive what you are looking for.
WooCommerce - Disable the default stylesheet
Disable all stylesheets
WooCommerce enqueues 3 stylesheets by default. You can disable them all with the following snippet:
add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
Disable specific stylesheets
If you want to disable specific stylesheets (i.e, if you do not want to include the handheld stylesheet), you can use the following:
add_filter( 'woocommerce_enqueue_styles', 'jk_dequeue_styles' );
function jk_dequeue_styles( $enqueue_styles ) {
unset( $enqueue_styles['woocommerce-general'] );
unset( $enqueue_styles['woocommerce-layout'] );
unset( $enqueue_styles['woocommerce-smallscreen'] );
return $enqueue_styles;
}
add_filter( 'woocommerce_enqueue_styles', '__return_false' );
Then enqueue your own stylesheet like so:
function wp_enqueue_woocommerce_style(){
wp_register_style( 'mytheme-woocommerce', get_stylesheet_directory_uri() . '/css/woocommerce.css' );
if ( class_exists( 'woocommerce' ) ) {
wp_enqueue_style( 'mytheme-woocommerce' );
}
}
add_action( 'wp_enqueue_scripts', 'wp_enqueue_woocommerce_style' );
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.
How do I remove a sub-menu with link
http://vagrant.local/wp/wp-admin/admin.php?page=home_settings_page ?
I tried remove_submenu_page( 'admin.php', 'yrc_home_settings_page' );
but that didn't work.
Edit
function remove_menu_pages_for_fuel_surcharge_editor() {
if(current_user_can('fuel-surcharge-editor')) {
remove_menu_page('tools.php');
remove_menu_page('options-general.php');
remove_menu_page('edit.php?post_type=show_event');
remove_menu_page('jetpack');
remove_submenu_page( 'admin.php', 'yrc_home_settings_page' );
}
}
add_action('admin_menu', 'remove_menu_pages_for_fuel_surcharge_editor', 999);
I also tried which didn't work either.
add_action('admin_init', 'remove_menu_pages_for_fuel_surcharge_editor', 999);
Screenshot:
I think you will need to add the function to a hook. Try something like:
function remove_submenu() {
remove_submenu_page( 'admin.php', 'yrc_home_settings_page' );
}
add_action( 'admin_menu', 'remove_submenu', 999 );
I'm not entirely sure if the second parameter in your function is correct.
I don't want the customers to get linked to the myaccount-dashboard with the annoying Hello xy, from here you can blabla, when they click on "My Account".
Is there an endpoint for the my account dashboard? I didn't find it on the backend in woocommerce>Settings>Accounts...
What's working is: I set up a custom link under menu/navigation... called it "My Account" and set the link to /myaccount/downloads for example.
So, when customers are logged in and they click on My Account, they get redirected to Downloads.
I'm wondering, is there another way to get rid of the dashboard?
Or a redirect solution? Thanks.
Remove it using below function. change downloads to your requirement.
function custom_my_account_menu_items( $items ) {
unset($items['downloads']);
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items' );
Looking more closely it seems like there are a handful of ways to do this. Take a look at the my-account.php template. You could override this, but I found that if you remove the woocommerce_account_content hook, you get a deprecation notice and WooCommerce thinks you have an outdated template and adds the content anyway.
Looking at the template you will see two hooks. The side menu navigation is added to the woocommerce_account_navigation and the content is added to woocommerce_account_content function. You can remove the defaults from their hooks and add back just the downloads content.
function so_41983566_remove_account_dadshboard(){
remove_action( 'woocommerce_account_navigation', 'woocommerce_account_navigation' );
remove_action( 'woocommerce_account_content', 'woocommerce_account_content' );
add_action( 'woocommerce_account_content', 'so_41983566_download_content' );
}
add_action( 'woocommerce_account_navigation', 'so_41983566_remove_account_dadshboard', 1 );
function so_41983566_download_content(){
do_action( 'woocommerce_account_downloads_endpoint' );
}
Or woocommerce_account_content() and woocommerce_account_navigation() are both pluggable functions and you can just define new versions in your theme/plugin.
This link explains everything:
https://github.com/woocommerce/woocommerce/wiki/Customising-account-page-tabs
First you need to create an endpoint:
function my_custom_endpoints() {
add_rewrite_endpoint( 'my-custom-endpoint', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'my_custom_endpoints' );
function my_custom_query_vars( $vars ) {
$vars[] = 'my-custom-endpoint';
return $vars;
}
add_filter( 'query_vars', 'my_custom_query_vars', 0 );
Then create the menu item:
function my_custom_my_account_menu_items( $items ) {
$logout = $items['customer-logout'];
unset( $items['customer-logout'] );
$items['my-custom-endpoint'] = __( 'My Custom Endpoint', 'woocommerce' );
$items['customer-logout'] = $logout;
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'my_custom_my_account_menu_items' );
Add content to the endpoint
function my_custom_endpoint_content() {
echo '<p>Hello World!</p>';
}
add_action( 'woocommerce_account_my-custom-endpoint_endpoint', 'my_custom_endpoint_content' );
I can't seem to get my functions to work for changing the excerpt_more filter of the Twenty Eleven parent theme.
I suspect it might actually be add_action( 'after_setup_theme', 'twentyeleven_setup' ); that's the problem, but I've even tried remove_filter( 'excerpt_more', 'twentyeleven_auto_excerpt_more' ) to get rid Twenty Eleven's function and still my functions aren't changing anything...
Can you help?
Here's the functions.php code in full:
http://pastie.org/3758708
Here's the functions I've added to /mychildtheme/functions.php
function clientname_continue_reading_link() {
return ' ' . __( 'Read more... <span class="meta-nav">→</span>', 'clientname' ) . '';
}
function clientname_auto_excerpt_more( $more ) {
return ' …' . clientname_continue_reading_link();
}
add_filter( 'excerpt_more', 'clientname_auto_excerpt_more' );
Thanks,
Osu
Ok, so after much frustration, I found a solution to this (I thought Child Themes were meant to speed things up!?). I believe this works because 'after_theme_setup' is run once the parent theme has been set up meaning you can remove / override Twenty Eleven's functions at that point.
If I've understood correctly, according to this documentation, the Child Theme is run first, then the parent and then the 'after_theme_setup' bit of code in your Child Theme's functions.php file:
http://codex.wordpress.org/Child_Themes#Using_functions.php
and
http://codex.wordpress.org/Plugin_API/Action_Reference/after_setup_theme
This is what's in my Child Theme's functions.php file, hope this helps someone:
// ------------------------------------------------------------------
// // !AFTER_SETUP_THEME
// ------------------------------------------------------------------
/* Set up actions */
add_action( 'after_setup_theme', 'osu_setup' );
if ( ! function_exists( 'osu_setup' ) ):
function osu_setup() {
// OVERRIDE : SIDEBAR GENERATION FUNCTION - NO WIDGETS FOR THIS SITE
remove_action( 'widgets_init', 'twentyeleven_widgets_init' ); /* Deregister sidebar in parent */
// OVERRIDE : EXCERPT READ MORE LINK FUNCTION
function osu_readon_link() {
return '...' . __( 'Read More...', 'clientname' ) . '';
}
// Function to override
function osu_clientname_custom_excerpt_more( $output ) {
if ( has_excerpt() && ! is_attachment() ) {
// $output = trim($output);
$output .= osu_readon_link();
}
return $output;
}
remove_filter( 'get_the_excerpt', 'twentyeleven_custom_excerpt_more' );
add_filter( 'get_the_excerpt', 'osu_clientname_custom_excerpt_more' );
remove_filter( 'excerpt_more', 'twentyeleven_auto_excerpt_more' );
add_filter( 'excerpt_more', 'osu_readon_link' );
// OVERRIDE : EXCERPT LENGTH FUNCTION
function osu_clientname_excerpt_length( $length ) {
return 30;
}
remove_filter( 'excerpt_length', 'twentyeleven_excerpt_length' );
add_filter( 'excerpt_length', 'osu_clientname_excerpt_length' );
}
endif; // osu_setup
Your own answer is complicating things and really shouldn't be necessary. I couldn't explain the why of my answers, because I found it in another answer. But in any case you can ALWAYS override functions from the parent theme in your child theme, although sometimes indeed you to use the remove_filter() beforehand, OR, like in this case, all you have to do is increase the priority of your added filter, in your case:
add_filter( 'excerpt_more', 'clientname_auto_excerpt_more', 11 );
That should do the trick. If not, increase the number.
Thanks to this answer: