Skip to content

Instantly share code, notes, and snippets.

@mishterk
mishterk / define-google-maps-api-key-for-acf.php
Last active August 20, 2023 19:16
Register Google Maps API Key for ACF
<?php
// Define this in the site's wp-config.php file.
define('GOOGLE_API_KEY', 'your-google-api-key-here');
// Add this to your functions.php file, or a config plugin/MU plugin.
add_filter( 'acf/fields/google_map/api', function ( $api ) {
$api['key'] = GOOGLE_API_KEY;
return $api;
@mishterk
mishterk / control-acf-menu-visibility.php
Created September 17, 2021 04:31
Hide the ACF admin menu dynamically based on site domain
<?php
add_filter( 'acf/settings/show_admin', function () {
// Get the current site url.
$site_url = get_bloginfo( 'url' );
// Define an array of protected site urls.
$protected_urls = array(
'https://www.example.com',
'http://staging.example.com'
@mishterk
mishterk / acf-field-group-php-to-json.php
Last active June 21, 2024 19:28
Convert an ACF Field Group from PHP to ACF-JSON
<?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 / 0-readme.md
Last active September 3, 2021 16:22
How to store ACF field values for WordPress attachments in custom database tables.

Using ACF Custom Database Tables for attachment meta

The only thing stopping the use of ACF Custom Database Tables for attachment fields is the logic we currently have in place that limits the use of the UI depending on a field group's location rules. The plugin expects either a post type or a user location rule and if it can't find one, it won't allow a table to be enabled for the field group in question so, when an Attachment location rule is used, the creation of a DB table is blocked as there is no post type in the location rules array.

This can be worked around by simply adding the attachment post type to the Post Type location rule values list, as per the code snippet below.

You can then set up the location rules as follows:

![ACF Location Rules](https://gist.github.com/mishterk/fab2cd441c873b1884a0e446

Controlling JSON encoded flags in ACF Custom Database Tables version 1.0.x

The ACF Custom Database Tables plugin uses JSON encoding to prepare complex values for storing in the database. This differs to WordPress' core approach as WordPress relies on serialization instead. By default, the plugin opts to not escape slashes when encoding but there may be times where additional flags need to be used. e.g; JSON_UNESCAPED_UNICODE.

The following example demonstrates how to short-circuit the plugin's encoding algorithm in order to define custom args for the wp_json_encode() function. It uses the acfcdt/filter_value_before_encode filter found in \ACFCustomDatabaseTables\Intercept\ACFInterceptBase::maybe_encode().

This example is JSON only

Note that this example is using JSON encoding which is what the plugin expects when attempting to decode data for return from the database.

@mishterk
mishterk / wp-query-efficiency-args.php
Created June 24, 2021 23:33
Some handy considerations when running WP_Query to speed up the query depending on the requirements. This demonstrates using specific post ID's from ACF custom database tables in conjunction with some built in WP_Query args to save on internal queries.
<?php
$query = new WP_Query([
// Standard query args. Using post__in can be much faster. If searching ACF custom
// database tables data, plugin the found post IDs in here.
'post_type' => 'post',
'post__in' => [1,2,3], // array of post IDs
// Optional args to improve performance. Use these to cut down on internal
@mishterk
mishterk / clone-gutenberg-reusable-block.php
Last active May 3, 2021 23:10
A function that clones an existing reusable block a given number of times.
<?php
/**
* Clone a given reusable block a given number of times.
*
* @param int $block_id The ID of an existing reusable block to clone.
* @param int $n_clones The number of clones to create.
*/
function clone_reusable_block( $block_id, $n_clones ) {
$post = get_post( $block_id );
@mishterk
mishterk / change-the-response-type-to-a-redirect.js
Last active April 30, 2021 00:40
AJAX functionality examples for Advanced Forms Pro version 1.8.0
jQuery(function ($) {
// Changing the response type to a redirect on the fly.
acf.addAction('af/form/ajax/submission', function (data, form) {
if (form.key === 'form_5f8f987654e0') {
data.type = 'redirect';
data.redirect_url = 'https://example.com';
}
});
});
@mishterk
mishterk / 0-readme.md
Last active December 30, 2020 22:32
Dynamically set the ACF JSON directory so that each sub-site in a WordPress multisite network has a separate acf-json dir. This maximises compatibility with ACF Custom Database Tables.

Using ACF JSON on WordPress multisite installations

ACF JSON can get a little messy where you need different field groups across your sub-sites. If you are using separate themes or separate child themes for each sub-site, you'll be fine as long as each theme/child-theme has its own /acf-json directory.

If, however, you want to enforce unique acf-json directories for each sub-site, you can do so using one of the following snippets.

  1. Dynamic ACF JSON directory
@mishterk
mishterk / PostTypeTaxonomyMirror.php
Created December 17, 2020 00:47
Post type/taxonomy mirror.
<?php
class PostTypeTaxonomyMirror {
private $post_type;
private $taxonomy;