I have a wordpress website with a theme.
To implement some changes I created a child theme. That works fine.
Now I want to add a page template that allows me to enqueue styles via wp_enqueue_style. For that to work I need to add wp_head() to my page template if I understand it correctly.
I want to use this custom page template for a front end app that I am creating (plugin).
The design of this app is completely separate from the rest of the website. Right now I get all the theme styles too when I use wp_head(). I would like to prevent the default theme styles from loading.
What is the most easy way to achieve this? Preferably a theme independent solution.
You can include a different header.
https://codex.wordpress.org/Function_Reference/get_header
Multiple Headers
Different header for different pages.
<?php
if ( is_home() ) :
get_header( 'home' );
elseif ( is_404() ) :
get_header( '404' );
else :
get_header();
endif;
?>
I did some digging in Google and came up with something that does what I want.
After clearing I can add my own styles and scripts.
function clear_styles_and_scripts() {
global $wp_scripts;
global $wp_styles;
foreach( $wp_scripts->queue as $handle ) :
wp_dequeue_script( $handle );
wp_deregister_script( $handle );
endforeach;
foreach( $wp_styles ->queue as $handle ) :
wp_dequeue_style( $handle );
wp_deregister_style( $handle );
endforeach;
}
add_action( 'wp_enqueue_scripts', 'clear_styles_and_scripts', 100 );
Related
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 );
Hey I have created tabbed menu in my wordpress blog. I have done it in parent theme. Now I have created a child theme of that parent theme. Everything seems fine expect tabbed menu. I have used jQuery for tabbed menu.
Here is my function.php file's specific part to include jQuery and css code from parent theme.
// enqueue tabs script
function my_scripts_tabs() {
if ( !is_admin() ) {
wp_enqueue_script('jquery-ui-tabs');
wp_enqueue_script( 'custom-tabs', get_stylesheet_directory_uri() .
'/js/tabs.js', array('jquery'));
}
}
add_action('wp_enqueue_scripts', 'my_scripts_tabs');
//add tabs stylesheet
wp_register_style('jquery-custom-style',
get_stylesheet_directory_uri().'/css/jquery-ui-1.12.1.custom/jquery-ui.css',
array(), '1', 'screen');
wp_enqueue_style('jquery-custom-style');
Here is my function.php file for child theme.
<?php
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED - Do not modify or remove comment markers above or below:
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( 'genericons' ) );
}
endif;
add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css', 10 );
// END ENQUEUE PARENT ACTION
Anyone please help. TIA
You need to edit
get_stylesheet_directory_uri().'/css/jquery-ui-1.12.1.custom/jquery-ui.css'
to
get_template_directory_uri().'/css/jquery-ui-1.12.1.custom/jquery-ui.css'
at parent theme.
Because when it is run by child theme get_stylesheet_directory_uri() returns child theme path, although the code is in parent theme.
I'm trying load a style sheet is a user role is subscriber, what am I doing wrong:
<?php
global $current_user;
get_currentuserinfo();
if ( user_can( $current_user, "subscriber" ) ){
echo '<link rel="stylesheet" href="/login/subscriber-style.css">'
}
?>
This is in the header.php of the theme
You're using an outdated method of getting the current user role. get_currentuserinfo() was deprecated in WordPress 4.5.
https://codex.wordpress.org/Function_Reference/get_currentuserinfo
Also, you shouldn't be loading CSS directly from your header.php file, you need to enqueue it instead.
Add the following code to your functions.php file.
function wpse_load_subscriber_stylesheet() {
if ( current_user_can( 'subscriber' ) ) {
wp_enqueue_style( 'login-subscriber-style', home_url( '/login/subscriber-style.css' ) );
}
}
add_action( 'wp_enqueue_scripts', 'wpse_load_subscriber_stylesheet' );
This assumes that your CSS is placed in your web root (https://example.com/login/subscriber-style.css). If it's in your theme folder instead then you need get_template_directory_uri() . '/login/subscriber-style.css'.
https://developer.wordpress.org/reference/functions/wp_enqueue_style/
I'm a beginer on wordpress, and my job will be to integrate templates and themes.
But a have a question with the wp_head and wp_footerfunctions.
Both of them generate stylesheet and scripts, but on my theme and my template page and would like only one stylesheet and one script (ex : css/style.css and js/app.js) .
I mean, style.css will have template rules but already styles from plugin etc .
And app.js will have jQuery minified, severals plugins (like lightbox etc) and my own script .
How can i do that ? I normally use grunt or gulp . But can i say ton wp functions "don't add scripts from plugins in the head, but add it in my main JS file " ?
Best regards
Scripts that are loaded through wp_head or wp_footer are enqueued in Wordpress.
If you want to remove those and instead load only your one single stylesheet and one single JS file, then you will need to dequeue all of those other scripts.
First, however, you need to identify which scripts are enqueued, which you can do by loading the global $wp_styles and $wp_scripts variables and iterating through them like so:
function se_inspect_styles() {
global $wp_styles;
echo "<h2>Enqueued CSS Stylesheets</h2><ul>";
foreach( $wp_styles->queue as $handle ) :
echo "<li>" . $handle . "</li>";
endforeach;
echo "</ul>";
}
add_action( 'wp_print_styles', 'se_inspect_styles' );
function se_inspect_scripts() {
global $wp_scripts;
echo "<h2>Enqueued JS Scripts</h2><ul>";
foreach( $wp_scripts->queue as $handle ) :
echo "<li>" . $handle . "</li>";
endforeach;
echo "</ul>";
}
add_action( 'wp_print_scripts', 'se_inspect_scripts' );
Then you can manually dequeue and deregister all those scripts by hooking into the wp_print_styles action (for CSS files) and the wp_print_scripts action (for JS files) like below:
// Dequeue CSS
function se_dequeue_unnecessary_styles() {
wp_dequeue_style( 'bootstrap-map' );
wp_deregister_style( 'bootstrap-map' );
}
add_action( 'wp_print_styles', 'se_dequeue_unnecessary_styles' );
// Dequeue JS
function se_dequeue_unnecessary_scripts() {
wp_dequeue_script( 'modernizr-js' );
wp_deregister_script( 'modernizr-js' );
wp_dequeue_script( 'project-js' );
wp_deregister_script( 'project-js' );
}
add_action( 'wp_print_scripts', 'se_dequeue_unnecessary_scripts' );
If you still need any of the dequeued scripts, then just be sure to add their source to your grunt / gulp file.
Lastly, you'll want to enqueue your one single CSS and JS scripts produced by grunt / gulp (although you can always just link to these manually from your templates if you feel like it).
Here is some example code for doing so:
function se_theme_js(){
// Theme JS
wp_register_script( 'my-scripts',
get_template_directory_uri() . '/js/main.min.js',
array('jquery'),
null,
true );
wp_enqueue_script('my-scripts');
}
add_action( 'wp_enqueue_scripts', 'se_theme_js' );
function se_theme_styles() {
// Theme CSS
wp_register_style( 'my-style',
get_stylesheet_directory_uri() . '/css/main.min.css',
array(),
null,
'all' );
wp_enqueue_style( 'my-style' );
}
add_action( 'wp_enqueue_scripts', 'se_theme_styles' );
I'm trying to customize WooCommerce external product links to open in new tabs...
This is my try:
added a filter to the WordPress theme functions.php file as the following:
add_filter( 'woocommerce_product_add_to_cart_url', 'woocommerce_externalProducts_openInNewTab' );
function woocommerce_externalProducts_openInNewTab($product_url) {
global $product;
if ( $product->is_type('external') ) {
$product_url = $product->get_product_url() . '"target="_blank""';
}
return $product_url;
}
What did I missed?
what you're currently doing is wrong... get_product_url is named as what it do. It will give you the url... not the html anchor that has the url, but just the url.. so you're just adding some text to the url.. that's what you are doing...
One solution is given by #Ash Patel. You can change the markup by using templates... just navigate to your plugin folder and look for this file.. woocommerce\templates\single-product\add-to-cart\external.php. You can find instructions inside it.
Now, sometimes, we don't like editing templates... especially if it's just minor edits like this...
Below code will do it the way you want it... just paste this code in your theme's functions.php.
remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30 );
add_action( 'woocommerce_external_add_to_cart', 'rei_external_add_to_cart', 30 );
function rei_external_add_to_cart(){
global $product;
if ( ! $product->add_to_cart_url() ) {
return;
}
$product_url = $product->add_to_cart_url();
$button_text = $product->single_add_to_cart_text();
do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<p class="cart">
<?php echo esc_html( $button_text ); ?>
</p>
<?php do_action( 'woocommerce_after_add_to_cart_button' );
}
Here is how you add target="_blank" to the links on archive pages:
function ns_open_in_new_tab($args, $product)
{
if( $product->is_type('external') ) {
// Inject target="_blank" into the attributes array
$args['attributes']['target'] = '_blank';
}
return $args;
}
add_filter( 'woocommerce_loop_add_to_cart_args', 'ns_open_in_new_tab', 10, 2 );
Replace ns_ part with your own namespace abbreviation.
Remove above funtion from function.php:
Use plugin files from Template folder by Template Overwrite method and then
follow below path:
woocommerce\templates\single-product\add-to-cart\external.php
open external.php where there is a tag, apply target="_blank".
it will work.