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 / avoid_loading_unused_assets.php
Created September 29, 2022 00:20
Do not load unused assets [WordPress]
//Do not load plugins' scripts and styles on every page - only where they are in use
add_filter( 'style_loader_tag', 'sz_stop_loading_unused_styles', 10, 2 ); // *doesn't works with 3 arguments
function sz_stop_loading_unused_styles( $tag, $handle) {
if (!is_user_logged_in()){// for FE only
if (is_singular('post')) {// for Posts only
//entire plugin
if (str_contains($tag, 'woocommerce')) $tag = '';
//specific handle
//Gutenberg
if (in_array( $handle, ['wc-blocks-style', 'wp-block-editor', 'wp-editor', 'wp-block-library', 'wp-components'])) $tag = '';
@sabrina-zeidan
sabrina-zeidan / get_acf_block_data.php
Last active April 30, 2024 06:14
Get ACF field value from Gutenberg block [WordPress]
// Get ACF value whetherit comes from regular ACF or ACF block
// Useful if you need to access ACF fileds that are in block as regular fields
// Usage: get_acf_block_data($post, 'acf/talk-description', 'talk_description' )
function get_acf_block_data($post, $block_name = 'acf/default-block-name', $field_name = "" ){
$content = "";
if ( has_blocks( $post->post_content ) && !empty($field_name )) {
$blocks = parse_blocks( $post->post_content );
foreach($blocks as $block){
if ( $block['blockName'] === $block_name ) {
if(isset($block["attrs"]["data"][$field_name ])){
@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 June 2, 2024 19:08
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( 'render_block', 'sz_embed_gists_in_gutenberg_block', 10, 2 );
function sz_embed_gists_in_gutenberg_block( $block_content, $block ) {
// Check if the block is a paragraph block
if ( $block['blockName'] !== 'core/paragraph' ) {
return $block_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);