Skip to content

Instantly share code, notes, and snippets.

View blogjunkie's full-sized avatar

David Wang blogjunkie

View GitHub Profile
@blogjunkie
blogjunkie / woocommerce_variation_is_active.php
Created February 1, 2021 05:55 — forked from plugin-republic/woocommerce_variation_is_active.php
Grey out variations that are out of stock for WooCommerce
/**
* Disable out of stock variations
* https://github.com/woocommerce/woocommerce/blob/826af31e1e3b6e8e5fc3c1004cc517c5c5ec25b1/includes/class-wc-product-variation.php
* @return Boolean
*/
function wcbv_variation_is_active( $active, $variation ) {
if( ! $variation->is_in_stock() ) {
return false;
}
return $active;
@blogjunkie
blogjunkie / functions.php.php
Created February 19, 2021 03:30
Add custom fonts to Elementor from your theme
<?php // Don't include this line
/**
* Add new font group (Custom) to the top of the list
*/
add_filter( 'elementor/fonts/groups', function( $font_groups ) {
$new_font_group = array( 'custom' => __( 'Custom' ) );
return array_merge( $new_font_group, $font_groups );
} );
@blogjunkie
blogjunkie / functions.php
Created May 12, 2021 02:25
Add a note to WooCommerce checkout
<?php // don't copy this line
/**
* Add a note to WooCommerce checkout
*
* This snippet adds a message to the checkout screen. You can have the message appear at different location
* by changing the `woocommerce_review_order_before_payment` hook to any other hook. Refer to:
* https://www.businessbloomer.com/woocommerce-visual-hook-guide-checkout-page/
*/
@blogjunkie
blogjunkie / index.html
Created June 13, 2021 14:17
Mimic defer script for inline JavaScript
<script>
// Load only after DOMContentLoaded event
window.addEventListener('DOMContentLoaded', function() {
(function($) {
// do something
})(jQuery);
});
</script>
@blogjunkie
blogjunkie / script.js
Created June 24, 2021 07:41
Move an element into a different div
// Move `.element` into `#inner-wrap`
// The code has to appear after the elements, otherwise it will throw an error
const moveMe = document.querySelector('#main .element');
const target = document.querySelector('#inner-wrap');
target.appendChild(relatedPosts);
@blogjunkie
blogjunkie / functions.php
Last active September 30, 2021 02:19
Preload site logo and LCP image in WordPress
<?php // don't copy this line
/**
* Modify the $image_id variable and replace 'medium_large' with desired image size
* See: https://web.dev/optimize-lcp/?utm_source=lighthouse&utm_medium=unknown#preload-important-resources
* Note: don't go overboard and preload too many assets, just ones that appear above the fold
*/
add_action( 'wp_head', 'child_preload_assets', 1 );
@blogjunkie
blogjunkie / functions.php.php
Created November 11, 2021 12:12
Disable PowerPack outputting data-no-lazy in Content Grid module
<?php // don't copy this line
add_filter( 'pp_post_image_settings_data', function( $data, $settings ) {
if ( isset( $data['attributes'] ) && isset( $data['attributes']['data-no-lazy'] ) ) {
unset( $data['attributes']['data-no-lazy'] );
}
return $data;
}, 10, 3 );
@blogjunkie
blogjunkie / functions.php
Created January 12, 2022 08:15
Pre-fill ACF repeater sub-fields
<?php
/**
* Let's say we have an ACF repeater field with 2 sub-fields:
*
* Section
* - Title
* - Description
*
* This code snippet will automatically load 3 instances of the Section, and pre-fill the titles.
@blogjunkie
blogjunkie / functions.php.php
Created June 23, 2022 05:58
Restore Classic Widgets
<?php // Don't copy this line
function example_theme_support() {
remove_theme_support( 'widgets-block-editor' );
}
add_action( 'after_setup_theme', 'example_theme_support' );
@blogjunkie
blogjunkie / functions.php.php
Created June 7, 2023 08:05
Disable page title in Hello theme if Elementor is active
<?php // Don't copy this line
function ele_disable_page_title( $return ) {
$post_id = get_the_ID();
$elementor = Elementor\Plugin::$instance->documents->get( $post_id )->is_built_with_elementor();
if ( $elementor == 1 ) {
return false;
}
else {
return $return;