Skip to content

Instantly share code, notes, and snippets.

View lukecav's full-sized avatar
Infinite data generation engine

Luke Cavanagh lukecav

Infinite data generation engine
View GitHub Profile
@lukecav
lukecav / functions.php
Created May 25, 2023 20:51
Add HTTP security headers in WordPress
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
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
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
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
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
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
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
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
<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>
@lukecav
lukecav / Commands
Last active May 8, 2023 20:29
Useful commands to get a list of all admin users, reset the password for all admin users, shuffle the salts and them update plugins and themes using WP-CLI commands
wp user list --role=administrator --format=csv
wp user reset-password $(wp user list --fields=ID --role=administrator)
wp user reset-password $(wp user list --fields=ID --role=administrator) -skip-email
wp config shuffle-salts
wp plugin update --all
wp theme update --all