Skip to content

Instantly share code, notes, and snippets.

@mishterk
mishterk / 0-readme.md
Last active July 1, 2020 11:21
A basic example for querying data from custom tables created using ACF Custom Database Tables. For more info see https://hookturn.io/2019/09/how-to-use-acf-custom-database-tables-data-with-wp_query-objects/

How to use your custom table data with WP_Object queries

This example illustrates how to query an array of post IDs from a custom DB table then use the array in a WP_Query.

This can be much faster than using meta queries on a WP_Query object, particularly if you are matching multiple fields.

<?php
add_action( 'wp_enqueue_scripts', function () {
$uri = get_stylesheet_directory_uri();
$dir = get_stylesheet_directory();
$script_last_updated_at = filemtime( "$dir/scripts/my-script.js" );
$style_last_updated_at = filemtime( "$dir/styles/my-style.css" );
.m-0-first-last > :first-child {
margin-top: 0;
}
.m-0-first-last > :last-child {
margin-bottom: 0;
}
@mishterk
mishterk / get-acf-sub-field-key-by-field-name-function.php
Last active October 1, 2019 22:27
A function for determining an ACF field's sub field key based on a field name. For more info see https://philkurth.com.au/tips/get-an-acf-sub-field-key-by-field-name/
<?php
/**
* Locates an ACF sub-field by field name and returns the sub-field's key.
*
* This is particularly useful if you need to construct a data array for programmatic field
* update where a complex field is in use (e.g; repeater, group, flexi).
*
* @param string $sub_field_name The sub field name we need a key for.
* @param array $field The ACF field array.
<?php
/*
* This is a rather simple and contrived example but it illustrates how this
* function can be used to temporarily change the post context.
*/
$alt_post_content = override_post_context( 1234, function ( $post ) {
ob_start();
// Do whatever you need here. The $post variable, in this scope, is the
@mishterk
mishterk / customise-advanced-forms-mail-payload.php
Created October 4, 2019 23:48
Intercept and modify the payload sent by Advanced Forms
<?php
// Set the form key you wish to target
$form_key = 'form_5d97cf9edc0a8';
add_action( "af/email/before_send/key=$form_key", function ( $email, $form ) {
add_filter( 'wp_mail', function ( $data ) use ( $email ) {
// you can override any items in this array to customise the email that is sent...
<?php
add_filter( 'acf/settings/load_json', function ( array $directories ) {
$base_dir = get_stylesheet_directory() . '/components';
$resource = opendir( $base_dir );
while ( ( $file = readdir( $resource ) ) !== false ) {
// Skip current and parent directory references
<?php
add_filter( 'acf/fields/wysiwyg/toolbars', function ( $toolbars ) {
// Register a basic toolbar with a single row of options
$toolbars['Custom One'][1] = [ 'bold', 'italic', 'underline', 'forecolor', 'link', 'unlink' ];
// Register another toolbar, this time with two rows of options.
$toolbars['Custom Two'][1] = [ 'bold', 'italic', 'underline', 'strikethrough', 'forecolor', 'wp_adv' ];
$toolbars['Custom Two'][2] = [ 'bullist', 'numlist', 'alignleft', 'aligncenter', 'alignright' ];
<?php
add_action( 'admin_footer', function () {
?>
<script>
if (window.acf) {
acf.addFilter('color_picker_args', function (args, $field) {
args.palettes = [
'#E6D8D5',
@mishterk
mishterk / define-custom-colour-palette-for-gutenberg.php
Last active October 14, 2019 09:57
How to define custom colour palette for Gutenberg; the new WordPress block editor. More info see https://philkurth.com.au/tips/how-to-customize-the-colour-palette-for-wordpress-gutenberg-block-editor/
<?php
add_action( 'after_setup_theme', function () {
add_theme_support( 'editor-color-palette', [
[
'name' => 'Purple',
'slug' => 'purple',
'color' => '#2B265C'
],