Skip to content

Instantly share code, notes, and snippets.

<?php
add_action( 'af/field_group/before_field_group', function ( $field_group, $form, $args ) {
if ( $form['key'] !== 'form_64c0bb30bd954' ) {
return;
}
echo '<div class="my-field-group-wrapper">';
@mishterk
mishterk / commit.sh
Last active July 14, 2023 03:12
How to remove Git tracking for third party plugins in WordPress. Snippets for the post https://philkurth.com.au/articles/remove-git-tracking-third-party-wordpress-plugins/(opens in a new tab)
git commit -m "Stopped tracking third party plugins"
<?php
// The field accepts a value with this structure
$value = [
'address' => '123 Example St, Townsville XYZ 1234, Country',
'lat' => - 38.1486228,
'lng' => 144.360414,
'zoom' => 14,
'place_id' => 'Ei0xMjMgTW9vcmFib29sIFN0LCBHZWVsb25nIFZJQyAzMjIwLCBBdXN0cmFsaWEiMBIuChQKEgmX0JaIHBTUahFyH_LC9sYD8hB7KhQKEglDz-DYDxTUahECZY8QisCjzg',
'street_number' => 123,

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.

<?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 ) {
<?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
[
{
"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
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/
<?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 );
@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/
<?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();