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 / 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']) ) {
@sabrina-zeidan
sabrina-zeidan / display_all_hooks.php
Created November 19, 2021 11:24
Display all hooks/actions fired during the current page load [WordPress]
class MyTracker {
static $hooks;
static function track_hooks( ) {
$filter = current_filter();
if ( ! empty($GLOBALS['wp_filter'][$filter]) ) {
foreach ( $GLOBALS['wp_filter'][$filter] as $priority => $tag_hooks ) {
foreach ( $tag_hooks as $hook ) {
if ( is_array($hook['function']) ) {
@sabrina-zeidan
sabrina-zeidan / defer_all_css.php
Created October 19, 2021 10:16
Defer all CSS in WordPress (with exclusions)
function sz_defer_all_css_but( $tag, $handle ) {
if (is_admin()) { return $tag; }
$handlestoexclude = [];
if ( !in_array($handle, $handlestoexclude ) ){
$dom = new \DOMDocument();
$dom->loadHTML($tag);
$csstag = $dom->getElementById($handle . '-css');
$csstag->setAttribute('media', 'print');
$csstag->setAttribute('onload', "this.media='all'");
$csstag->removeAttribute('type');
@sabrina-zeidan
sabrina-zeidan / exclude_first_img_from_ll_wpr.php
Last active April 13, 2023 09:59
Exclude first image on single post from WP Rocket lazyload
//if not using rocket_lazyload_excluded_attributes filter, class remove-lazy should be added to WP Rocket settings in admin!
// + there is a helper plugin to do so in case we HAVE unique attribute to refer to https://github.com/wp-media/wp-rocket-helpers/blob/master/lazyload/wp-rocket-exclude-x-first-images-by-attribute/p
add_filter ('the_content', 'sz_add_class_to_first_img');
function sz_add_class_to_first_img($content){
if (!is_admin() && !is_user_logged_in()){ //do only on frontend for not logged-in
if ( is_singular('post') ) { //do on single posts only
$content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
$document = new DOMDocument();
libxml_use_internal_errors(true);
@sabrina-zeidan
sabrina-zeidan / delay_intercom_wordpress.php
Created September 25, 2021 09:23
Delay Intercom execution in WordPress
// Intercom plugin adds Intercom script to critical render path, sets it to be not defered
// And adds the script in WordPress in a way it doesn't leave much options to alter it in order to delay Intercom execution and prevent it to be render blocking
// Altering buffer in WP generally in not a great idea, but since we have no choice, let's make sure we are altering it in a way it doesn't break things
// If you use WP Rocket -- use 'rocket_buffer' filter instead
add_action( 'init', 'sz_buffer_process_post' ); //we use 'init' action to use ob_start()
function sz_buffer_process_post() {
ob_start();
}
@sabrina-zeidan
sabrina-zeidan / find_acf_fields_by_type.php
Last active September 13, 2021 22:26
Find all ACF image fields used on the site (including subfields) [WordPress]
//For example we need to modify all <img> tags in the templates that use ACF img fields.
//To locate them in the theme we need to know their names.
//This snippet will list the names of all ACF fields of image type used on the site, so that we'll be able to search for them in the theme files
$field_groups = acf_get_field_groups();
$all_fields = array();
foreach ($field_groups as $field_group){
$fields = acf_get_fields($field_group['key']);
$all_fields = array_merge($all_fields, $fields);
}
class Alter_WP_Scripts extends WP_Scripts{
//also $deps method!
function do_item( $handle, $group = false ){
if( 'handle_here' == $handle ){
$handle = false;
}
return parent::do_item( $handle, $group );
}
}
if( !is_admin() ){
@sabrina-zeidan
sabrina-zeidan / filter_wp_scripts_via_hook.php
Last active September 6, 2022 06:23
To move third-party JS out of the head seamlessly
//add_action( 'wp_print_scripts', 'sz_enqueue_scripts', 10); //run later or via wp_print_scripts ot catch all scripts
add_action( 'wp_enqueue_scripts', 'sz_enqueue_scripts', 999);
function sz_enqueue_scripts(){
global $wp_scripts;
//change dependency to load earlier
$handles_to_filter = array('dt-above-fold');
$dependency_handle_to_add = 'wpb_composer_front_js'; //TODO: accept array
foreach ($handles_to_filter as $handle_to_filter){
foreach( $wp_scripts->registered as $script_object) { //find script in registered by handle
if ($script_object->handle == $handle_to_filter){
@sabrina-zeidan
sabrina-zeidan / empty_plugin_with_a_search_field.php
Created March 27, 2021 12:39
Empty plugin with a search field for WP REST API tut
<?php
/**
* Plugin Name: SZ WP REST API Tutorial
* Description: It's an example of WP REST API usage for search with autocomplete with vanilla JS
* Plugin URI:
* Author: Sabrina Zeidan
* Author URI: https://sabrinazeidan.com
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*