Skip to content

Instantly share code, notes, and snippets.

@mishterk
mishterk / LocalValetDriver.php
Last active April 20, 2023 15:18
A local Valet driver for proxying images to a remote host
View LocalValetDriver.php
<?php
/**
* Class LocalValetDriver
*
* This class demonstrates how we might go about proxying any missing local images to a remote host. i.e; the production
* site. This has been created with WordPress in mind but could be adjusted to work with any other system.
*/
class LocalValetDriver extends WordPressValetDriver {
View 0-readme.md

Limit number of checkable boxes

Here is a simple JavaScript code snippet that limits the number of checkboxes that can be checked at any one time. In this example, the limit is set to 3 checkboxes.

This code creates a simple HTML page with 5 checkboxes. When a user tries to select more than the allowed number of checkboxes (in this case, 3), an alert will be displayed, and the latest checkbox selection will be undone.

View mark-wordpress-scripts-as-async-or-defer.php
<?php
add_action( 'wp_enqueue_scripts', function () {
wp_register_script( 'my-script', get_stylesheet_directory_uri() . '/assets/js/my-script.js' );
wp_enqueue_script( 'my-script' );
} );
add_filter( 'script_loader_tag', function ( $tag, $handle ) {
View use-acfs-load-field-filter-with-advanced-forms.php
<?php
add_filter( 'acf/load_field/key=TARGET_FIELD_KEY_HERE', 'afp_test_acf_load_field_filter' );
function afp_test_acf_load_field_filter( $field ) {
$field['choices'] = [];
// Using get_posts() instead of WP_Query prevents query loop issues and global var overrides that break
// functionality on the advanced forms edit screen and potentially other contexts.
$posts = get_posts( [
@mishterk
mishterk / acf-export-group_63799935b6476.json
Last active November 30, 2022 22:58
Testing multiple calculated fields in Advanced Forms Pro for ACF. These resources are for the video: https://www.loom.com/share/fcf6179162374c599b61eb6a1040eb02
View acf-export-group_63799935b6476.json
[
{
"key": "group_63799935b6476",
"title": "Calculated Fields",
"fields": [
{
"key": "field_637999367d114",
"label": "Number 1",
"name": "number_1",
"aria-label": "",
@mishterk
mishterk / count-new-members-between-two-dates.sql
Last active October 9, 2022 21:23
Helpful SQL queries for working with the MemberPress WordPress plugin
View count-new-members-between-two-dates.sql
SELECT count(DISTINCT txns.user_id)
FROM (
SELECT
user_id,
min(created_at) AS first_txn_created_at
FROM wp_mepr_transactions
WHERE status IN ('complete', 'confirmed')
GROUP BY user_id
) AS first_txns
INNER JOIN wp_mepr_transactions AS txns
@mishterk
mishterk / filter-acf-relationship-field-post-titles.php
Last active September 18, 2022 23:16
How to show the age of each post in an ACF relationship field. For more info see https://philkurth.com.au/tips/customise-the-post-titles-in-acf-relationship-field/
View filter-acf-relationship-field-post-titles.php
<?php
add_filter( 'acf/fields/relationship/result/name=related_posts', function ( $title, WP_Post $post, $field_arr ) {
$posted_at = get_post_time( 'U', false, $post->ID );
$now = current_time( 'timestamp' );
$diff = human_time_diff( $posted_at, $now );
return $title . sprintf( ' (%s ago)', $diff );
}, 10, 3 );
View 0-woocommerce-snippets.md

WooCommerce Snippets

Just a handy collection of small snippets for customising WooCommerce.

@mishterk
mishterk / acf-field-group-php-to-json.php
Last active August 29, 2022 00:27
Convert an ACF Field Group from PHP to ACF-JSON
View acf-field-group-php-to-json.php
<?php
// Get all the local field groups and loop through them for processing.
$field_groups = acf_get_local_field_groups();
foreach ( $field_groups as $group ) {
// If the field group has fields, load them into the 'fields' key.
if ( acf_have_local_fields( $group['key'] ) ) {
$group['fields'] = acf_get_local_fields( $group['key'] );
}
@mishterk
mishterk / load-images-from-production.php
Last active July 14, 2022 06:26
WordPress plugin for loading images from production on a staging/development website. See https://hookturn.io/load-media-images-from-production-wordpress-plugin/
View load-images-from-production.php
<?php
/**
* Plugin Name: Load Images From Production (for staging/dev)
* Description: Hooks into WP's media URL generation and replaces the domain with the production domain.
* Author: Phil Kurth
* Author URI: https://hookturn.io
*/
// If this file is called directly, abort.
defined( 'WPINC' ) or die();