Skip to content

Instantly share code, notes, and snippets.

@elliotcondon
Last active February 13, 2022 14:42
Show Gist options
  • Save elliotcondon/f0f3141f34d1e57fc55387574c49262c to your computer and use it in GitHub Desktop.
Save elliotcondon/f0f3141f34d1e57fc55387574c49262c to your computer and use it in GitHub Desktop.
A Closer Look at Applying Filter Variations in ACF.
function acf_get_value( $post_id, $field ) {
// Load meta value.
$value = acf_get_metadata( $post_id, $field['name'] );
/**
* Filters the $value after it has been loaded.
*
* @date 28/09/13
* @since 5.0.0
*
* @param mixed $value The value loaded.
* @param string $post_id The post ID for this value.
* @param array $field The field array.
*/
$value = apply_filters( "acf/load_value/type={$field['type']}", $value, $post_id, $field );
$value = apply_filters( "acf/load_value/name={$field['name']}", $value, $post_id, $field );
$value = apply_filters( "acf/load_value/key={$field['key']}", $value, $post_id, $field );
$value = apply_filters( "acf/load_value", $value, $post_id, $field );
// return
return $value;
}
/**
* Filters the $value after it has been loaded.
*
* @date 28/09/13
* @since 5.0.0
*
* @param mixed $value The value loaded.
* @param string $post_id The post ID for this value.
* @param array $field The field array.
*/
foreach( array('type', 'name', 'key', '') as $k ) {
$sufix = $k ? "/$k={$field[$k]}" : '';
$value = apply_filters( "acf/load_value$sufix", $value, $post_id, $field );
}
function acf_get_value( $post_id, $field ) {
// Load meta value.
$value = acf_get_metadata( $post_id, $field['name'] );
/**
* Filters the $value after it has been loaded.
*
* @date 28/09/13
* @since 5.0.0
*
* @param mixed $value The value loaded.
* @param string $post_id The post ID for this value.
* @param array $field The field array.
*/
$value = apply_filters( "acf/load_value", $value, $post_id, $field );
// return
return $value;
}
// Add variations to filter.
add_filter( "acf/load_value", "_acf_apply_load_value_filters", 10, 3 );
// Apply variations to filter.
function _acf_apply_load_value_filters( $value, $post_id, $field ) {
$value = apply_filters( "acf/load_value/type={$field['type']}", $value, $post_id, $field );
$value = apply_filters( "acf/load_value/name={$field['name']}", $value, $post_id, $field );
$value = apply_filters( "acf/load_value/key={$field['key']}", $value, $post_id, $field );
return $value;
}
function acf_get_value( $post_id, $field ) {
// Load meta value.
$value = acf_get_metadata( $post_id, $field['name'] );
/**
* Filters the $value after it has been loaded.
*
* @date 28/09/13
* @since 5.0.0
*
* @param mixed $value The value loaded.
* @param string $post_id The post ID for this value.
* @param array $field The field array.
*/
$value = apply_filters( "acf/load_value", $value, $post_id, $field );
// return
return $value;
}
// Register variation.
acf_add_filter_variations( 'acf/load_value', array('type', 'name', 'key'), 2 );
/**
* acf_add_filter_variations
*
* Registers variations for the given filter.
*
* @date 26/1/19
* @since 5.7.11
*
* @param string $filter The filter name.
* @param array $variations An array variation keys.
* @param int $index The param index to find variation values.
* @return void
*/
function acf_add_filter_variations( $filter = '', $variations = array(), $index = 0 ) {
// Store replacement data.
acf_get_store('hook-variations')->set( $filter, array(
'type' => 'filter',
'variations' => $variations,
'index' => $index,
));
// Add generic handler.
// Use a priotiry of 10, and accepted args of 10 (ignored by WP).
add_filter( $filter, '_acf_apply_hook_variations', 10, 10 );
}
// Register store.
acf_register_store('hook-variations');
/**
* acf_add_filter_variations
*
* Registers variations for the given filter.
*
* @date 26/1/19
* @since 5.7.11
*
* @param string $filter The filter name.
* @param array $variations An array variation keys.
* @param int $index The param index to find variation values.
* @return void
*/
function acf_add_filter_variations( $filter = '', $variations = array(), $index = 0 ) {
// Store replacement data.
acf_get_store('hook-variations')->set( $filter, array(
'type' => 'filter',
'variations' => $variations,
'index' => $index,
));
// Add generic handler.
// Use a priotiry of 10, and accepted args of 10 (ignored by WP).
add_filter( $filter, '_acf_apply_hook_variations', 10, 10 );
}
/**
* acf_add_action_variations
*
* Registers variations for the given action.
*
* @date 26/1/19
* @since 5.7.11
*
* @param string $action The action name.
* @param array $variations An array variation keys.
* @param int $index The param index to find variation values.
* @return void
*/
function acf_add_action_variations( $action = '', $variations = array(), $index = 0 ) {
// Store replacement data.
acf_get_store('hook-variations')->set( $action, array(
'type' => 'action',
'variations' => $variations,
'index' => $index,
));
// Add generic handler.
// Use a priotiry of 10, and accepted args of 10 (ignored by WP).
add_action( $action, '_acf_apply_hook_variations', 10, 10 );
}
/**
* _acf_apply_hook_variations
*
* Applys hook variations during apply_filters() or do_action().
*
* @date 25/1/19
* @since 5.7.11
*
* @param mixed
* @return mixed
*/
function _acf_apply_hook_variations() {
// Get current filter.
$filter = current_filter();
// Get args provided.
$args = func_get_args();
// Get variation information.
$variations = acf_get_store('hook-variations')->get( $filter );
extract( $variations );
// Find field in args using index.
$field = $args[ $index ];
// Loop over variations and apply filters.
foreach( $variations as $variation ) {
// Get value from field.
// First look for "backup" value ("_name", "_key").
if( isset($field[ "_$variation" ]) ) {
$value = $field[ "_$variation" ];
} elseif( isset($field[ $variation ]) ) {
$value = $field[ $variation ];
} else {
continue;
}
// Apply filters.
if( $type === 'filter' ) {
$args[0] = apply_filters_ref_array( "$filter/$variation=$value", $args );
} else {
do_action_ref_array( "$filter/$variation=$value", $args );
}
}
// Return first arg.
return $args[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment