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
//Add missing width and height to the specific img to avoid layout shift, knowing its parent element class | |
//Or modify anything at all via WP Rocket buffer | |
add_filter('rocket_buffer','sz_wprocket_modify_buffer', 10); | |
function sz_wprocket_modify_buffer ($buffer){ | |
$dom = new DOMDocument(); | |
$dom->loadHTML($buffer); | |
$xpath = new DOMXPath($dom); | |
$expression = './/div[contains(concat(" ", normalize-space(@class), " "), " mypic ")]'; //tag and class here | |
foreach ($xpath->evaluate($expression) as $parentelement) { | |
foreach ($parentelement->getElementsByTagName('img') as $tag) { //targeting img inside the parent tag |
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
//Known issue in Safari 15.4, happens in WPR as well as in other LL plugins | |
//https://github.com/wp-media/wp-rocket/issues/4961 | |
//Removes loading="lazy" by filtering output buffer in case it wasn't removed in the regular way | |
add_filter('rocket_buffer', 'sz_wprocket_filter_buffer_native_ll', 10); | |
function sz_wprocket_filter_buffer_native_ll ($buffer){ | |
$buffer = str_replace('loading="lazy"', '', $buffer); | |
return $buffer; | |
} |
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 |
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
//Important: When adding a gist link in Editor you MUST choose - convert to link, not Embed! | |
//Embed Gists to be displayed in Gutenberg without plugin, works on new and existing ones | |
add_filter( 'the_content', 'sz_embed_gists_in_gutenberg' ); | |
function sz_embed_gists_in_gutenberg( $content ) { | |
if(empty($content)) return; | |
$dom = new DOMDocument; | |
libxml_use_internal_errors(true); | |
$content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8"); | |
$dom->loadHTML($content); |
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
//Disable native WP lazyloading for images by adding a CSS class lazyload-disabled in Block Editor | |
//Now let's make the magic happen | |
add_filter('wp_img_tag_add_loading_attr', 'sz_disable_ll', 10, 3); | |
function sz_disable_ll($value, $image, $context){ | |
$disabled_class = 'lazyload-disabled'; | |
if (false !== strpos($image, $disabled_class)) { | |
return false; | |
} | |
return $value; |
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
//In a better world block's author makes sure the block's assests are loaded only if block is actually in use (via enqueue_block_assets). For other cases: | |
add_filter( 'script_loader_tag', 'sz_stop_loading_unused_block_crap', 10, 3 ); | |
function sz_stop_loading_unused_block_crap( $tag, $handle, $src ) { | |
// Check if block's assets are loaded | |
if (str_contains($src, 'plugins/wp-swiper')) { //plugin directory here | |
if (is_singular()) {// works only on Singular | |
//TODO: //Will work for content only. if block is elsewhere or post it's archive use this https://wordpress.stackexchange.com/questions/392493/find-if-widget-block-is-active | |
$id = get_the_ID(); | |
if (has_block('da/wp-swiper-slide', $id) !== true) { //block name here |
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
//rocket_buffer in theory should work no matter if gallery/slider is in the_content or not, inserted via js etc | |
//they don't have class and it's not possibleto assignit specifically to img elements -- here is what we can do | |
//we need to know: smallest parent element that contains them all | |
//checked -- it doesn't impact those exclusions added via ui | |
add_filter('rocket_buffer', 'sz_exclude_images_without_class_from_ll'); | |
function sz_exclude_images_without_class_from_ll($buffer) { | |
$document = new DOMDocument(); | |
libxml_use_internal_errors(true); |
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
//For testing purposes | |
if (!is_user_logged_in() ) { | |
function sz_wprocket_filter_buffer($buffer) | |
{ | |
$jetpack_cdn = 'https://i1.wp.com/'; | |
$buffer = preg_replace('/((http|https)\:\/\/)(([^\"|^\)|^\']*?)(?:.jpg|.jpeg|.png))/im', $jetpack_cdn . '${3}', $buffer); | |
return $buffer; | |
} | |
add_filter('rocket_buffer', 'sz_wprocket_filter_buffer', 10); |
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
// Preload the first image of the homepage dynamically | |
// In case homepage section that is displayed above the fold is modified this will preload the needed image | |
add_action( 'wp_head', 'sz_preload_first_image'); | |
function sz_preload_first_image() { | |
if( is_front_page() ) { | |
if ( have_rows( 'home_sections' ) ) { | |
while ( have_rows( 'home_sections' ) ) : the_row(); | |
if (get_row_layout() == 'book') { | |
$content = get_sub_field( 'field_5c70983b528e8' ); |
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
//From here https://gist.github.com/azizultex/dd55c5c0518a427ae3a5552f683eb29b Modified -> Mind the comment to make it work with WP > 4.7 | |
function remove_filters_for_anonymous_class( $hook_name = '', $class_name ='', $method_name = '', $priority = 0 ) { | |
global $wp_filter; | |
// Take only filters on right hook name and priority | |
if ( !isset($wp_filter[$hook_name][$priority]) || !is_array($wp_filter[$hook_name][$priority]) ) | |
return false; | |
// Loop on filters registered | |
foreach( (array) $wp_filter[$hook_name][$priority] as $unique_id => $filter_array ) { | |
// Test if filter is an array ! (always for class/method) | |
if ( isset($filter_array['function']) && is_array($filter_array['function']) ) { |