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 / 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
*
@sabrina-zeidan
sabrina-zeidan / optimize_a2a.php
Created January 27, 2021 20:08
Optimize Add2Any loading it's styles and scripts
//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;
}
}
@sabrina-zeidan
sabrina-zeidan / acf_sync_with_user_profile.php
Last active March 22, 2023 10:12
When you have ACF User fields like first name and last name and you need to keep that in sync with what is entered in WordPress user profile (works both ways)
//If both changed at the same time - custom field value will be applied as it fires later, set 30 to change it add_action( 'profile_update', array($this, 'update_acf_fields'), 30, 2 );
class Sync_ACF_with_User_Profile_Class {
static public $sync_pair = array(
array( 'acf' => 'member_name', 'profile_field' => 'first_name'), //add acf field name according to your setup
array( 'acf' => 'member_lastname', 'profile_field' => 'last_name')// add more rows to sync any other fields as well
);
public function __construct() {
add_action( 'profile_update', array($this, 'update_acf_fields'), 10, 2 ); //when profile is updated -> update ACF
add_action('updated_user_meta', array($this, 'update_user_profile_fields'),10,4); // when ACF is updated -> update profile
add_action('added_user_meta', array($this, 'update_user_profile_fields'),10,4); // when ACF is added -> update profile