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 / 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 / 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
@sabrina-zeidan
sabrina-zeidan / sz_repeat_purchase_wtf.php
Last active February 23, 2023 01:20
Disable repeat purchase WooCommerce [WordPress]
/** 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;
@sabrina-zeidan
sabrina-zeidan / wordpress_multisite_unfiltered_html.php
Created December 7, 2018 17:25
Let users apart from Superadmin insert raw code in text editor [WordPress Multisite]
//Let any user who can edit page insert unfiltered html in text editor
add_action( 'init', 'reply_unfiltered_html' );
function reply_unfiltered_html() {
$user = wp_get_current_user();
if ( current_user_can('edit_pages') )
kses_remove_filters();
}
//Let users with certain roles insert unfiltered html in text editor
add_action( 'init', 'reply_unfiltered_html_for_roles' );
function reply_unfiltered_html_for_roles() {
@sabrina-zeidan
sabrina-zeidan / webp_for_site.conf
Last active January 9, 2023 19:43
Serve WebP without plugin straight from the server
upstream php {
{{#each fastcgi_servers}}
server {{this}};
{{/each}}
}
# SZ START
# Check if client is capable of handling webp
map $http_accept $webp_suffix {
default "";
@sabrina-zeidan
sabrina-zeidan / delete_empty_terms.php
Last active December 28, 2022 22:17
Bulk delete empty taxonomy terms [Wordpress]
function delete_empty_terms(){
$taxonomy_name = 'city';
$terms = get_terms( array(
'taxonomy' => $taxonomy_name,
'hide_empty' => false
) );
foreach ( $terms as $term ) {
$term_count = $term->count;
if ($term_count < 1){ wp_delete_term($term->term_id, $taxonomy_name);
}
@sabrina-zeidan
sabrina-zeidan / delete_cpt_attachments.php
Last active October 22, 2022 14:13
Delete all attachments attached to specified custom posts (card in this case) [Wordpress]
function delete_cpt_attachments(){
$attachments = get_posts( array(
'post_type' => 'attachment',
'numberposts' =>-1,
));
if ($attachments) {
foreach ($attachments as $attachment){
$parent_id = $attachment->post_parent;
if ( 'card' == get_post_type($parent_id) ) {
$attachmentID = $attachment->ID;
@sabrina-zeidan
sabrina-zeidan / generate_alt_from_title.php
Created October 2, 2022 20:50
Generate image alt from the post title -- for the first image in the post [WordPress]
// 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]);
@sabrina-zeidan
sabrina-zeidan / gists_in_gutenberg.php
Last active September 28, 2022 17:55
Paste gist.github.com URL in Gutenberg -> get the embed [WordPress]
//Important: When adding a gist link in Editor you MUST choose - convert to link, not Embed!
//Embed Gists to be displayed in Gutenberg without plugin, works on new and existing ones
add_filter( 'the_content', 'sz_embed_gists_in_gutenberg' );
function sz_embed_gists_in_gutenberg( $content ) {
if(empty($content)) return;
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
$dom->loadHTML($content);
@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){