Skip to content

Instantly share code, notes, and snippets.

View af-field-group-wrapper-hooks.php
<?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)
View commit.sh
git commit -m "Stopped tracking third party plugins"
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 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 / 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();
@mishterk
mishterk / replace-images-with-kittens.php
Last active July 14, 2022 04:25
Replace all attachment images with kitten placeholders...
View replace-images-with-kittens.php
<?php
add_filter( 'wp_get_attachment_image_src', function ( $image ) {
$image[0] = "https://placekitten.com/$image[1]/$image[2]";
return $image;
}, 10 );
View register-custom-admin-columns.php
<?php
$post_type = 'my_post_type';
// Register the columns.
add_filter( "manage_{$post_type}_posts_columns", function ( $defaults ) {
$defaults['custom-one'] = 'Custom One';
$defaults['custom-two'] = 'Custom Two';
View how-to-add-where-clause-to-wp-query-1.php
<?php
// Hook the filter before calling the query. Note that we are using an anonymous
// function here and saving a reference to the `$fn` variable for use further down.
add_filter( 'posts_where', $fn = function ( $where, WP_Query $wp_query ){
global $wpdb;
// Add a clause that ensures we only get posts with an ID greater than 200.
$where .= " AND {$wpdb->posts}.ID > 200";
View use-acf-field-as-fallback-for-excerpt.php
<?php
add_filter( 'get_the_excerpt', function ( $excerpt, $post ) {
if ( ! empty( $excerpt ) ) {
return $excerpt;
}
// On a specific post type, use an ACF field value as the excerpt.
if ( $post->post_type === 'my_custom_post_type' ) {
$excerpt = get_field( 'product_description', $post->ID );