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
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']) ) { |
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
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'); |
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
//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); |
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
// 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(); | |
} |
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 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); | |
} |
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
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() ){ |
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_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){ |
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
<?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 | |
* |
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
//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; | |
} | |
} |
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
//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 |