Skip to content

Instantly share code, notes, and snippets.

Avatar
🔹
Semantic compression

Luke Cavanagh lukecav

🔹
Semantic compression
View GitHub Profile
@lukecav
lukecav / functions.php
Created May 26, 2023 20:09
Remove the sales price and use regular price when the stock is zero in WooCommerce
View functions.php
// Remove sale price and use regular price when stock is 0
function remove_sale_price_on_zero_stock( $price, $product ) {
// Check if the product is on sale and has stock
if ( $product->is_on_sale() && $product->get_stock_quantity() <= 0 ) {
$regular_price = $product->get_regular_price();
$price = wc_price( $regular_price ); // Use regular price instead of sale price
}
return $price;
}
add_filter( 'woocommerce_product_get_price', 'remove_sale_price_on_zero_stock', 10, 2 );
@lukecav
lukecav / functions.php
Created May 25, 2023 20:51
Add HTTP security headers in WordPress
View functions.php
function add_security_headers($headers) {
// Add X-XSS-Protection header
$headers['X-XSS-Protection'] = '1; mode=block';
// Add X-Content-Type-Options header
$headers['X-Content-Type-Options'] = 'nosniff';
// Add X-Frame-Options header
$headers['X-Frame-Options'] = 'SAMEORIGIN';
@lukecav
lukecav / functions.php
Created May 24, 2023 19:39
Only reduce stock inventory on completed orders in WooCommerce
View functions.php
add_action( 'init', 'custom_wc_maybe_reduce_stock_levels', 10 );
function custom_wc_maybe_reduce_stock_levels(){
// remove_action( 'woocommerce_order_status_completed', 'wc_maybe_reduce_stock_levels' );
remove_action( 'woocommerce_payment_complete', 'wc_maybe_reduce_stock_levels' );
remove_action( 'woocommerce_order_status_processing', 'wc_maybe_reduce_stock_levels' );
remove_action( 'woocommerce_order_status_on-hold', 'wc_maybe_reduce_stock_levels' );
add_action( 'woocommerce_payment_complete', 'wc_maybe_increase_stock_levels' );
add_action( 'woocommerce_order_status_processing', 'wc_maybe_increase_stock_levels' );
@lukecav
lukecav / Command
Created May 24, 2023 19:18
Run a site scan in iThemes Security Pro from WP-CLI command
View Command
wp itsec site-scanner scan
@lukecav
lukecav / functions.php
Created May 19, 2023 19:15
Stay logged in to wp-admin (WordPress Dashboard) for 48 hours in WordPress
View functions.php
function extend_auth_cookie_expiration( $expiration, $user_id, $remember ) {
// Set the desired cookie expiration time (48 hours in this example)
$cookie_expire = 2 * DAY_IN_SECONDS;
// Return the new expiration time
return $cookie_expire;
}
add_filter( 'auth_cookie_expiration', 'extend_auth_cookie_expiration', 10, 3 );
@lukecav
lukecav / Command
Created May 17, 2023 20:45
Update the Essential Addons for Elementor Lite plugin using a WP-CLI command
View Command
wp plugin update essential-addons-for-elementor-lite
@lukecav
lukecav / Command
Created May 15, 2023 20:32
Get the values of the iThemes Security Pro plugin settings from WP-CLI command
View Command
wp option get itsec-storage --format=json
@lukecav
lukecav / functions.php
Created May 10, 2023 21:29
Change product tag on stock change in WooCommerce
View functions.php
function action_woocommerce_low_stock( $wc_get_product ) {
// Product set tag id(s), multiple IDs can be added, separated by a comma
$wc_get_product->set_tag_ids( array( 'YOUR TAG ID' ) );
// OPTIONAL: Set category ids
//$wc_get_product->set_category_ids( array( 39, 2 ) );
// Save
$wc_get_product->save();
}
@lukecav
lukecav / functions.php
Created May 10, 2023 21:26
Fix the product tab is locked issue in WooCommerce 7.7.0
View functions.php
function reset_product_template( $post_type_args ) {
if ( array_key_exists( 'template', $post_type_args ) ) {
unset( $post_type_args['template'] );
}
return $post_type_args;
}
add_filter( 'woocommerce_register_post_type_product', 'reset_product_template' );
@lukecav
lukecav / .htaccess
Created May 9, 2023 19:28
mod_pagespeed .htaccess configuration
View .htaccess
<IfModule pagespeed_module>
ModPagespeed on
ModPagespeedEnableFilters recompress_jpeg,recompress_png
ModPagespeedEnableFilters recompress_webp
ModPagespeedEnableFilters convert_gif_to_png,convert_jpeg_to_progressive
ModPagespeedEnableFilters convert_jpeg_to_webp,convert_png_to_jpeg
ModPagespeedEnableFilters jpeg_subsampling
ModPagespeedEnableFilters strip_image_color_profile,strip_image_meta_data
</IfModule>