Skip to content

Instantly share code, notes, and snippets.

View sabrina-zeidan's full-sized avatar

Sabrina Zeidan sabrina-zeidan

View GitHub Profile
@sabrina-zeidan
sabrina-zeidan / modify_specific_element_via_wp_rocket_buffer.php
Created August 22, 2022 00:50
Modify any specific element knowing its parent class via WP Rocket buffer [WordPress]
//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
@sabrina-zeidan
sabrina-zeidan / wp_rocket_ll_images_missing_fix.php
Created August 20, 2022 16:32
Fix WP Rocket Lazyload not working properly on Safari/Chrome 15.4 if loading=lazy is present on the image [WordPress]
//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;
}
@sabrina-zeidan
sabrina-zeidan / a2a_clean_up.php
Created August 5, 2022 17:48
Prevent Add2Any CSS and JS from being loaded where it's not used [WordPress]
//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
@sabrina-zeidan
sabrina-zeidan / gists_in_gutenberg.php
Last active September 28, 2022 17:55
Paste gist.github.com URL in Gutenberg -> get the embed [WordPress]
//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);
@sabrina-zeidan
sabrina-zeidan / disable_wp_lazyloading.php
Last active September 6, 2022 06:23
Disable native WordPress / browser lazy loading by adding class
//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;
@sabrina-zeidan
sabrina-zeidan / stop_loading_unused_blocks _crap.php
Created July 13, 2022 16:00
Stop loading unused Gutenberg blocks assets [WordPress]
//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
@sabrina-zeidan
sabrina-zeidan / exclude_hard_images_from_ll_wprocket.php
Last active September 6, 2022 06:23
Exclude images without specific class/attribute from WP Rocket lazyload [WordPress]
//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);
@sabrina-zeidan
sabrina-zeidan / fetch_webp_from_jetpack_cdn_via_wprocket_buffer.php
Created February 3, 2022 12:14
Serve WEBP format from JetPack CDN using WP Rocket buffer output [WordPress]
//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);
@sabrina-zeidan
sabrina-zeidan / preload_1st_image_ACF_flexible.php
Created January 25, 2022 14:24
Preload 1st image set in ACF Flexible content
// 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' );
@sabrina-zeidan
sabrina-zeidan / remove_action_added_in_class.php
Last active September 6, 2022 06:23
Remove action added in parent theme in Class [WordPress]
//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']) ) {