Skip to content

Instantly share code, notes, and snippets.

@mishterk
Last active September 11, 2019 02:43
Show Gist options
  • Save mishterk/cddd8f96ab917d8a7f16af74d5d740cd to your computer and use it in GitHub Desktop.
Save mishterk/cddd8f96ab917d8a7f16af74d5d740cd to your computer and use it in GitHub Desktop.
A hotfixed solution for bypassing custom database tables when using ACF's get_field() function. This will ONLY work with 1.0.x versions of the plugin. Version 1.1 will include built-in support for this capability.
<?php
use ACFCustomDatabaseTables\Intercept\ACFGetFieldIntercept;
use ACFCustomDatabaseTables\Vendor\Pimple\Container;
/**
* Class ACFCDTvOneDotZeroDotAnyGetFieldInterceptBypass
*
* This provides a 'hotfixed' approach for disabling custom database table intercept when using ACF's get_field()
* function. This will only work with version 1.0.x versions of the plugin as a built-in tool will be available in
* version 1.1 and beyond.
*/
class ACFCDTvOneDotZeroDotAnyGetFieldInterceptBypass {
/**
* Disables the get field intercept allowing data retrieval from core meta tables only.
*/
public static function enable() {
if ( $instance = self::get_instance() ) {
remove_filter( 'acf/pre_load_value', [ $instance, 'fetch_value' ], 10 );
}
}
/**
* Enables the get field intercept allowing data retrieval from custom database tables.
*/
public static function disable() {
if ( $instance = self::get_instance() and ! has_filter( 'acf/pre_load_value', [ $instance, 'fetch_value' ] ) ) {
add_filter( 'acf/pre_load_value', [ $instance, 'fetch_value' ], 10, 3 );
}
}
/**
* Fetches the get field intercept instance, if available.
*
* @return ACFGetFieldIntercept|null
*/
private static function get_instance() {
$plugin = acf_custom_database_tables();
if ( ! method_exists( $plugin, 'container' ) ) {
return null;
}
$container = $plugin->container();
if ( ! $container instanceof Container ) {
return null;
}
$intercept_instance = $container['acf_get_field_intercept'];
if ( ! $intercept_instance instanceof ACFGetFieldIntercept ) {
return null;
}
return $intercept_instance;
}
}
<?php
require 'ACFCDTvOneDotZeroDotAnyGetFieldInterceptBypass.php';
// EXAMPLE 1:
// To disable get field intercepts entirely – which means all data is retreived from core meta
// tables only – you could simply call the following from your functions.php file or a
// configuration plugin:
ACFCDTvOneDotZeroDotAnyGetFieldInterceptBypass::enable();
// EXAMPLE 2:
// IF, however, you are in a template and only want to bypass certain fields as you call them,
// you can turn the bypass on and off as follows:
the_field('some_field'); // custom table will be queried
ACFCDTvOneDotZeroDotAnyGetFieldInterceptBypass::enable();
the_field('some_other_field'); // custom table won't be queried
ACFCDTvOneDotZeroDotAnyGetFieldInterceptBypass::disable();
the_field('some_third_field'); // custom table will be queried
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment