Skip to content

Instantly share code, notes, and snippets.

View BruceMcKinnon's full-sized avatar

BruceMcKinnon

View GitHub Profile
@BruceMcKinnon
BruceMcKinnon / functions.php
Created March 2, 2025 22:07
Enqueue CSS & JS with unique version numbers
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
// Grab CSS and JS urls.
$theme_styles = "/css/child-theme.css";
$theme_scripts = "/js/child-theme.js";
// Use filetime() as the version number - so each time you rebuild the CSS or JS, you get a new version number to bust the browser cache
// Note: we use the local path with filetime(), not a url
$css_vers = filemtime( get_stylesheet_directory() . $theme_styles );
@BruceMcKinnon
BruceMcKinnon / functions.php
Created January 27, 2025 23:18
Disable updating of a Wordpress Plugin(s)
//
// Disable updates to a plugin - solution found on https://wordpress.stackexchange.com/questions/397326/how-do-i-disable-an-update-for-a-specific-plugin
//
function AS_disable_plugin_updates( $value ) {
//create an array of plugins you want to exclude from updates ( string composed by folder/main_file.php)
$pluginsNotUpdatable = [
'plugin1/plugin.php',
'plugin2/plugin2.php'
];
@BruceMcKinnon
BruceMcKinnon / functions.php
Created October 16, 2024 05:26
Gravity Forms New Zealand phone number validation mask
add_filter( 'gform_phone_formats', 'nz_phone_format' );
// NZ phone format - 8, 9 or 10 digits long
function anz_phone_format( $phone_formats ) {
$phone_formats['nz'] = array(
'label' => 'New Zealand',
'mask' => '99 9999 99?99',
'regex' => '/^(?:(?:\+?64[2-9]|0[2-9])\d{6,7}|(?:\+?642\d{1}|02\d{1})\d{6,8}|(?:(?:\+?64800|0800)|(?:\+?64508|0508))\d{6,8})$/',
'instruction' => 'New Zealand phone numbers.',
);
return $phone_formats;
@BruceMcKinnon
BruceMcKinnon / functions.php
Created August 6, 2024 05:51
Support the serving of iframe content from a specific page
//
// Wordpress - Support for iframes being called for a specific page. Add this to functions.php
// In this case, the URL $allowed_url will be allowed to pull the contents of the 'my-embed' page
//
add_action( 'send_headers', 'add_header_xframe', 99 );
function add_header_xframe() {
if ( is_page('my-embed') ) {
$allowed_url = 'http://test.local';
header( 'Content-Security-Policy: frame-ancestors '.$allowed_url );
}
@BruceMcKinnon
BruceMcKinnon / functions.php
Created December 11, 2023 21:04
Ninja Forms Australian Phone number validation
//
// Hook the Ninja forms before submitting
//
add_filter( 'ninja_forms_submit_data', 'my_ninja_forms_submit_data' );
//
// This function hooks the Ninja submission step
// Thanks to https://stackoverflow.com/questions/61440656/ninja-forms-server-side-validation-not-working
//
@BruceMcKinnon
BruceMcKinnon / functions.php
Created November 3, 2023 00:51
Add ACF CPT products on-the-fly to a Gravity Form
//
// Shop Order Form - Allow shop products to be added to the GF on the fly.
//
// Products are defined in an ACF CPT. These products may be purchased via a Gravity Form.
// The base form should have all fields required, except without products.
// The product fields will be added immediately above the Shipping field.
//
add_filter( 'gform_pre_render_7', 'populate_shop_products' );
add_filter( 'gform_pre_validation_7', 'populate_shop_products' );
@BruceMcKinnon
BruceMcKinnon / functions.php
Created November 3, 2023 00:00
Add menu order support to posts and pages
add_action( 'init', 'ingeni_add_posts_menuorder' );
function ingeni_add_posts_menuorder() {
add_post_type_support( 'post, page', 'page-attributes' );
add_post_type_support( 'page', 'excerpt' );
}
@BruceMcKinnon
BruceMcKinnon / functions.php
Last active October 19, 2023 00:50
Check if a file in the WP uploads folder actually does exist
// Take the URL of a local file in the WP Uploads folder and check that it does actually exist
function uploaded_file_exists( $url ) {
$local_exists = false;
if ( $url ) {
$uploads = wp_upload_dir();
$local_path = str_replace( $uploads['baseurl'], $uploads['basedir'], $url );
$local_exists = file_exists($local_path);
}
@BruceMcKinnon
BruceMcKinnon / functions.php
Created September 14, 2023 06:57
WP menu custom container
//
// Custom WP menu container - thanks to https://gist.github.com/nikolov-tmw/8698598
//
// Add a custom container to your
//
add_action( 'admin_head-nav-menus.php', 'custom_register_menu_metabox' );
function custom_register_menu_metabox() {
$custom_param = array( 0 => 'mega_col' );
@BruceMcKinnon
BruceMcKinnon / functions.php
Last active July 19, 2023 23:44
Create a custom Yoast SEO Variable
//
// Add a Custom Yoast Variable
//
add_action('wpseo_register_extra_replacements', 'add_yoast_supercharge_variables');
function add_yoast_supercharge_variables() {
if (function_exists ('wpseo_register_var_replacement')){
wpseo_register_var_replacement('%%BRAND%%', 'supercharge_yoast_brand', 'advanced', 'Supercharge Brand' );
}
}