Skip to content

Instantly share code, notes, and snippets.

View blogjunkie's full-sized avatar

David Wang blogjunkie

View GitHub Profile
@blogjunkie
blogjunkie / functions.php
Created September 9, 2023 12:49
WordPress shortcode to send custom GA4 event - use it in the form confirmation message
<?php // Do not copy this line
/**
* Place this shortcode in the form confirmation message. Usage:
*
* [ga4_event] will send 'form_submit' event by default
* [ga4_event name="submit_enquiry"] to send 'submit_enquiry' event
* [ga4_event name="subscribed_to_newsletter"] to send 'subscribed_to_newsletter' event
*/
@blogjunkie
blogjunkie / functions.php
Created September 4, 2023 08:46
Exclude pages that have been noindex-ed in Rank Math from WP search
<?php // Do not copy this line
function child_exclude_noindex_from_search( $query ) {
if( $query->is_main_query() && $query->is_search() ) {
$meta_query = isset( $query->meta_query ) ? $query->meta_query : array();
$meta_query['noindex'] = array(
'key' => 'rank_math_robots',
'value' => "noindex",
@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;
@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
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 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
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 / 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 / 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 / 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/
*/