This file contains hidden or 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
// 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' ); |
This file contains hidden or 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
//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']) ) { |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 | |
* |