Created
August 5, 2022 17:48
-
-
Save sabrina-zeidan/f320180de8af995b1d3a04608a4486bb to your computer and use it in GitHub Desktop.
Prevent Add2Any CSS and JS from being loaded where it's not used [WordPress]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Prevent Add2Any CSS from being loaded | |
add_action( 'wp_print_styles','sz_a2a_optimize_styles', 10 ); | |
function sz_a2a_optimize_styles() { | |
if ( is_front_page() || is_archive() ) { | |
wp_dequeue_style('addtoany-inline'); | |
wp_dequeue_style('addtoany'); | |
} | |
} | |
//Prevent Add2Any JS from being loaded | |
add_action('wp_enqueue_scripts','sz_a2a_optimize_scripts'); | |
function sz_a2a_optimize_scripts($hook) { | |
if ( is_front_page() || is_archive() ) { | |
wp_dequeue_script('addtoany'); | |
wp_deregister_script('addtoany'); | |
} | |
} | |
//This filters display, but CSS and JS are still loaded | |
add_filter( 'addtoany_script_disabled', 'addtoany_disable_display' ); | |
function addtoany_disable_display($script_disabled) { | |
if ( is_front_page() || is_archive() ) { | |
return true; | |
} else { | |
return $script_disabled; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment