Skip to content

Instantly share code, notes, and snippets.

@mishterk
mishterk / load-flexi-field-layout-partials-in-templates.php
Created Sep 17, 2021
Split an ACF flexible content field into template parts
View load-flexi-field-layout-partials-in-templates.php
<?php
// In a post/page template, loop over the ACF flexible field layouts and load the partial
// responsible for rendering the layout.
while ( the_flexible_field('my_flexi_field') ) {
get_template_part( 'components/'. get_row_layout() );
}
@mishterk
mishterk / disable-core-meta-metabox.php
Created Sep 17, 2021
Speed up ACF backend loading time
View disable-core-meta-metabox.php
<?php
add_filter('acf/settings/remove_wp_meta_box', '__return_true');
@mishterk
mishterk / define-google-maps-api-key-for-acf.php
Last active Sep 17, 2021
Register Google Maps API Key for ACF
View define-google-maps-api-key-for-acf.php
<?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 Sep 17, 2021
Hide the ACF admin menu dynamically based on site domain
View control-acf-menu-visibility.php
<?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 Sep 17, 2021
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 / 0-readme.md
Last active Sep 3, 2021
How to store ACF field values for WordPress attachments in custom database tables.
View 0-readme.md

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

@mishterk
mishterk / wp-query-efficiency-args.php
Created Jun 24, 2021
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.
View wp-query-efficiency-args.php
<?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
View 0-readme.md

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.

View php-alternative-syntax-example.php
<?php
get_header();
?>
<div class="Main">
<?php if ( have_posts() ): ?>
<?php while ( have_posts() ): ?>
<?php the_post(); ?>
View class-view.php
<?php
class View {
public static $view_dir = '';
/**
* Render View Template With Data