remove style/scripts defined within plugin class - wordpress

Similar to this question WordPress - remove action defined within plugin class
i don't want to remove the whole action, if they update something, it will be overwrite.
I just want to remove one style enqueued in the class-wc-admin-assets.php.
But the file it's included inside a action with enclose.
So using this will not work:
function myFunctionToRemove(){
wp_dequeue_style('jquery-ui-style');
wp_deregister_style('jquery-ui-style');
}
add_action('admin_enqueue_scripts','myFunctionToRemove');
Is there a way to dequeue only one script?
The style that i want to dequeue it's 'jquery-ui-style' on class-wc-admin-assets.php

If the script is being enqueued on the admin_enqueue_scripts action you be able dequeue using your function, you likely just need to use a later priority.
function myFunctionToRemove(){
wp_dequeue_style('jquery-ui-style');
wp_deregister_style('jquery-ui-style');
}
add_action( 'admin_enqueue_scripts','myFunctionToRemove', 11 );
Try it with a priority of 11 and if that doesn't work try 999.

Related

woocommerce_checkout_create_order_line_item not overriding

woocommerce_checkout_create_order_line_item
This hook is already overridden in WooCommerce subscriptions but I want to make a few changes in it. so, I added the same hook in function file but not able to override the same. Is it even possible?
I tried the same in function file but didn't work.

Theme Action to hook for one time only function

I am developing a custom theme. After my theme is activated/installed I need to run a one time only function that will set some options. What is the best action to hook for this?
Should I use after_setup_theme? My understanding is that this event/action fires on each page load so I don't think I should use this action correct?
*Ps: is it best to place this code in my functions.php file or somewhere else? (I am using the underscores theme template).
You should look at after_switch_theme action. It'll fired only on your theme activation( just one time ). So, your one time function will look:
add_action('after_switch_theme', 'my_one_time_function');
function my_one_time_function () {
//Your code
}
Code goes to functions.php file.
P.S. If you want to run some function before deactivating theme, you can use switch_theme hook. It has same use as after_switch_theme.

WordPress Template "X": Change html inside of <head>

I'm pretty new to WordPress and want to remove some unneeded js/css files that are loaded inside of the html-head of the page.
In wp-content/themes/x/framework/views/header/base.php I found the following code:
<head>
<?php wp_head(); ?>
</head>
This obviously doesn't help me at all. I have no idea what WordPress' "wp_head()"-function does next. Isn't there some simple file where I can just write/edit simple HTML for the "head" somewhere?
wp_head() is a function that fires the wp_head action hook. Wordpress is built on action hooks and filters, which means that not only can plugins and themes add things to your setup through these hooks, but you have the ability to remove things as well. Most of this is done programmatically (in PHP) instead of there just being an html template, although that's not always the case... but it IS the case in your case.
Scripts and styles, for the most part, are enqueued through the wp_enqueue_scripts action hook. You'll want to perform your script removals here.
The problem is, you'll have to know the name the script was registered as before you can remove it. For instance, jQuery is registered as 'jquery'. Pretty simple, right? However, not all scripts are going to be that simple, and you'll have to browse through the code to find the registered handles (names) for those scripts. Styles are a bit easier, as when you inspect your page in the browser, styles will have an id attribute set to 'example-style-css'. Wordpress appends the '-css' to registered and enqueued styles, so the name of your style is actually 'example-style'.
In your theme's functions.php, you can dequeue scripts and styles so they won't be included on your page like so:
function stack_46669800_dequeue(){
wp_dequeue_script('the-script-you-want-to-remove');
wp_dequeue_style('the-style-you-want-to-remove');
}
add_action('wp_enqueue_scripts','stack_46669800_dequeue',100);
(Notice the 100 in the add_action function here... This is the 'priority', or the order in which all added actions are fired. Most are at 10, so I changed the priority here to 100 so this would be fired presumably later than whatever is enqueueing your unwanted scripts. You can't dequeue something that hasn't been enqueued yet.)

WordPress using different CSS - is this possible?

Bit is a basic question here but can someone confirm that this statement be confirmed: WordPress Pages (certain templates created within) can pull different CSS and JS?
Or - does WordPress only permit universal CSS + JS to be pulled across the entire site?
Thanks for clearing this up.
Depends on what plugin and themes you use. The WordPress/PHP functions wp_enqueue_style() and wp_enqueue_script() can be used literally by everyone (core, themes, plugins, you) to request WordPress to load styles or JavaSctript. You can combine this with WordPress functions to check whether the current page is something you want to filter for (post type, post, front-page, category archive, template, etc.). Here is an example to load a custom style if on front page :
if (is_front_page()) {
wp_enqueue_style('custom-frontpage', 'my/path/to/frontpage.css');
}
You will have to hook this piece of code to the wp_enqueue_script action so that WordPress executes it at the appropriate time. Here is an example using an anonymous function:
add_action('wp_enqueue_scripts', function() {
if (is_front_page())
wp_enqueue_style('custom-frontpage', 'my/path/to/frontpage.css');
});
You can also register your code as a "normal" function and pass the functions name to add_action() instead.
Edit: Enabling and disabling plugins is a bit more difficult, since you can never know how they implement their features without examining the source code. Here are my thoughts on this:
The plugin likely uses the above method (wp_enqueue_styles, wp_enqueue_scripts) to register it's styles and scripts. The plugin, since it assumes to be needed on all pages and posts, does this on every page without the conditional checking described earlier.
You could do one of the following to stop the plugin from doing this:
Identify the place where the plugin loads the styles and scripts and add the if-statement to only do so if the post-ID matches your desired post-ID. This method is bad since your changes are lost every time the plugin is updated.
Write a "counter plugin" (you could just add it to your theme or find a plugin that allowes you to add PHP to your page) that "dequeues" the style and script added by the plugin with inversed conditional tag
The counter-plugin approach would look as follows:
function custom_unregister_plugin() {
if (not the desired blog post) {
wp_dequeue_style('my-plugin-stylesheet-handle');
wp_dequeue_script('my-plugin-script-handle');
}
}
Make sure this function is executed after the enqueuing-code of your plugin by giving it a low priority in the same hook (999 is just an example, test it yourself):
add_action('wp_enqueue_scripts', 'custom_unregister_plugin', 999);
With wp_enqueue_style() you can add stylesheet (https://developer.wordpress.org/reference/functions/wp_enqueue_style/)
You can use it after detecting which template is used
function enqueue_custom_stylesheet() {
if(get_page_template() == 'contact.php')
wp_enqueue_style( 'contact-style', get_template_directory_uri().'/contact.css' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_custom_stylesheet' );
You can use wp_enqueue_style for your CSS, wp_enqueue_script for your JS, wp_localize_script to pass variables from PHP to JS.
You can call these with hooks like:
funtion enqueue_my_stuff()
{
// your enqueue function calls
}
add_action('wp_enqueue_scripts','enqueue_my_stuff'); //front end
add_action('admin_enqueue_scripts','enqueue_my_stuff'); //admin panel
add_action('login_enqueue_scripts','enqueue_my_stuff'); //login screen

WordPress, using child theme, how can I edit a function

I am using style.css to "add" styles to theme- so it won't get overwrite when i update the theme.
How can i accomplish the same effect with functions?
Suppose i have a function (in the parent theme),
function interio_gmap_shortcode($atts) {
....
....
$str .= '
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false**&language=iw**"></script>
This is what i want to add- &language=iw , it's not exist in the parent theme.
this function exist in shortcodes.php
How can I accomplish that?
If you're properly creating a child theme, you should have your own folder with a styles.css and a functions.php in it.
To override a function like you're describing, you'll need to copy the php function from its original location (either your parent theme's functions.php or shortcodes.php) then make the changes you need in your child theme's functions.php.
It depends on whether the parent theme was coded to allow the function to be overridden. If so, it will probably look something like:
if (!function_exists('interio_gmap_shortcode')) {
function interio_gmap_shortcode($atts) {
...
In this case, you can copy the function to your own functions.php file and change it. At the time that the parent functions.php runs, your function will already exist and will be the one used.
If the parent function.php is not coded something like this, then copying the function to your function.php will result in a "Duplicate Function" error.
In that case, you would need to create your own function with a different name and use that instead.

Resources