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 delete_transients_with_prefix( $prefix ) { | |
foreach ( get_transient_keys_with_prefix( $prefix ) as $key ) { | |
// delete_transient( $key ); | |
echo "<br>". $key." ".get_transient( $key ); | |
} | |
} | |
/** | |
* Gets all transient keys in the database with a specific prefix. | |
* |
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 goes in functions.php or plugin file or wherever | |
function dump_hook( $tag, $hook ) { | |
ksort($hook); | |
echo "<pre>>>>>>\t$tag<br>"; | |
foreach( $hook as $priority => $functions ) { | |
echo $priority; |
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
RewriteEngine On | |
# Check if the request is for a PNG or JPEG file | |
RewriteCond %{REQUEST_FILENAME} \.(png|jpe?g)$ | |
# Check if a WebP version of the file exists | |
RewriteCond %{REQUEST_FILENAME}.webp -f | |
# Redirect to the WebP version of the file | |
RewriteRule ^(.+)\.(png|jpe?g)$ $1.$2.webp [NC,L] |
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
//Measure performance of some WP funcitons | |
$startTime = microtime(true); | |
$guarded_pages = get_posts([ | |
'posts_per_page' => 100, | |
'no_found_rows' => true, | |
'post_type' => SpeedGuard_Admin::$cpt_name, | |
'post_status' => 'publish', | |
'fields' => 'ids' | |
] ); |
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
/** Disable repeat purchase V 22.20 7:40 -- on Adding to Cart check **/ | |
add_filter( 'woocommerce_add_to_cart_validation', 'limit_cart_items_from_category', 10, 5 ); | |
function limit_cart_items_from_category ( $passed, $product_id, $quantity, $variation_id = 0, $variations = null ){ | |
$current_product_id = ($variation_id === 0) ? $product_id : $variation_id; | |
// Loop through cart items checking if the product is already in cart | |
foreach ( WC()->cart->get_cart() as $cart_item ){ | |
if( $cart_item['data']->get_id() == $current_product_id ) { | |
wc_add_notice( __('This product is already in your cart.', 'woocommerce' ), 'error' ); | |
return false; |
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
upstream php { | |
{{#each fastcgi_servers}} | |
server {{this}}; | |
{{/each}} | |
} | |
# SZ START | |
# Check if client is capable of handling webp | |
map $http_accept $webp_suffix { | |
default ""; |
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
// Generate image alt from the post title -- for the first image in the post | |
add_filter('the_content', 'add_alt_tags', 99999); | |
function add_alt_tags($content) { | |
global $post; | |
preg_match_all('/<img[^>]+>/i', $content, $images); | |
if(!is_null($images)) { | |
foreach($images[0] as $index => $value) { | |
//If no alt | |
// if(!preg_match('/alt=/', $value)) { | |
$new_img = str_replace('<img', '<img alt="'.get_the_title().'"', $images[0][$index]); |
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
// Avoid loading unused assets, both JS and CSS | |
// Removes both CSS and JS | |
// For Full Site Editing Theme! | |
// 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 we can do it ourselves | |
// In this example I load Swiper's block assets only where the block is used | |
// TODO: Works for content on Singular content only. if block is elsewhere or it's archive use this https://wordpress.stackexchange.com/questions/392493/find-if-widget-block-is-active | |
add_filter( 'style_loader_tag', 'sz_stop_loading_unused_block_crap', 9999, 3 ); | |
add_filter( 'script_loader_tag', 'sz_stop_loading_unused_block_crap', 10, 3 ); | |
function sz_stop_loading_unused_block_crap( $tag, $handle, $src ) { |
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
//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 = ''; |
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
// 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 ])){ |
NewerOlder