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 / get_acf_block_data.php
Last active April 30, 2024 06:14
Get ACF field value from Gutenberg block [WordPress]
// 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 ])){
@sabrina-zeidan
sabrina-zeidan / test_queries_time.php
Last active February 12, 2024 19:06
Performance of a few common database queries [WordPress]
//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'
] );
@sabrina-zeidan
sabrina-zeidan / delete_404_attachments.php
Last active February 8, 2024 07:29
Clean Media Library from broken images. Delete attachments which files no longer available and return 404 error [Wordpress]
function delete_404_attachments(){
$attachments = get_posts( array(
'post_type' => 'attachment',
'numberposts' => -1,
'fields' => 'ids'
));
if ($attachments) {
foreach ($attachments as $attachmentID){
$file_url = wp_get_attachment_url( $attachmentID);
$file_headers = @get_headers($file_url);
@sabrina-zeidan
sabrina-zeidan / list_transients_by_prefix.php
Created December 22, 2023 12:43
List or delete all transients with a specific prefix WordPress
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.
*
@sabrina-zeidan
sabrina-zeidan / list_all_hooks.php
Created December 20, 2023 15:38
List all hooks firing in the order [WordPress]
//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;
@sabrina-zeidan
sabrina-zeidan / .hatccess
Created September 27, 2023 08:33
webp redirection on nexcess servers
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]
@sabrina-zeidan
sabrina-zeidan / avoid_loading_unused_assets.php
Created September 29, 2022 00:20
Do not load unused assets [WordPress]
//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 = '';
@sabrina-zeidan
sabrina-zeidan / stop_loading_blocks_crap_fse.php
Last active May 3, 2023 21:34
Stop loading block's CSS and JS if the block is not used -- for Full Site Edit themes [WordPress]
// 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 ) {
@sabrina-zeidan
sabrina-zeidan / remove_images_links_from_post_content_permanently.php
Created July 29, 2017 09:25
This function will delete links framing images in your post content permanently [Wordpress]
function remove_image_link_permanently(){
$all_posts = get_posts( array(
'post_type' => 'post',
'numberposts' => -1,
'status' => 'any'
));
foreach ($all_posts as $single_post){
$post_id = $single_post->ID;
$content = $single_post->post_content;
$updated_content =
@sabrina-zeidan
sabrina-zeidan / wordpress_multisite_global_menu_anywhere.php
Last active April 27, 2023 21:12
WordPress Multisite Global menu -- Get shared menu from the main site displayed anywhere in the WordPress Multisite network
//More use cases and how-to: https://sabrinazeidan.com/wordpress-multisite-global-menu-tips-create-display-customize/
if (in_array(get_current_blog_id(), array(3,4,6,9))){ //display on these subsites only
switch_to_blog( '1' ); //switch to the main site
if (is_nav_menu(16)){ //make sure that menu exists there
wp_nav_menu(
['menu' => '16', //grab menu with ID 16
'menu_class' => 'sz-global-menu-style', //add class to the output
]);
}