Created
March 5, 2025 20:54
-
-
Save RadGH/573bfe4007f23e1826a918058d9d3ba5 to your computer and use it in GitHub Desktop.
Advanced Custom Fields (ACF) - Adds a custom ACF location rule to field groups to choose "Specific Term" on taxonomy pages. This location rule is available to any field group, allowing you to show fields for a specific term within a taxonomy.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: RS ACF Specific Term Location | |
Description: Adds a custom ACF location rule "Specific Term". It lets you restrict a field group to show only for a specific term when editing a term in the backend. | |
Version: 1.0.2 | |
*/ | |
// category ancestor location rule | |
function rs_acf_location_types_specific_term( $choices ) { | |
if ( !isset( $choices['Forms']['specific_term_id'] ) ) { | |
$choices['Forms']['specific_term_id'] = 'Specific Term'; | |
} | |
return $choices; | |
} | |
function rs_acf_location_rule_values_specific_term( $choices ) { | |
$choices = acf_get_taxonomy_terms(); // from "Post Taxonomy" location | |
return $choices; | |
} | |
function rs_acf_location_rule_match_specific_term( $result, $rule, $screen ) { | |
// Most of this is from acf_location_post_taxonomy::rule_match | |
$s = function_exists('get_current_screen') ? get_current_screen() : false; | |
if ( !$s || !($s instanceof WP_Screen) ) return false; | |
if ( $s->base != 'term' ) return false; | |
$taxonomy = acf_maybe_get( $screen, 'taxonomy' ); | |
$current_term_id = acf_maybe_get( $_GET, 'tag_ID' ); | |
// bail if no term | |
if( !$current_term_id ) return false; | |
$current_term = get_term_by( 'term_id', (int) $current_term_id, $taxonomy ); | |
// bail if term not valid | |
if( !$current_term || is_wp_error($current_term) ) return false; | |
// get term data | |
$data = acf_decode_taxonomy_term( $rule['value'] ); | |
$term = get_term_by( 'slug', $data['term'], $data['taxonomy'] ); | |
// attempt get term via ID (ACF4 uses ID) | |
if( !$term && is_numeric($data['term']) ) { | |
$term = get_term_by( 'id', $data['term'], $data['taxonomy'] ); | |
} | |
// bail early if no term | |
if( !$term ) return false; | |
// match | |
$result = $term->term_id === $current_term->term_id; | |
// reverse if 'not equal to' | |
if( $rule['operator'] === '!=' ) { | |
$result = !$result; | |
} | |
// return | |
return $result; | |
} | |
add_filter( 'acf/location/rule_types', 'rs_acf_location_types_specific_term' ); | |
add_filter( 'acf/location/rule_values/specific_term_id', 'rs_acf_location_rule_values_specific_term' ); | |
add_filter( 'acf/location/rule_match/specific_term_id', 'rs_acf_location_rule_match_specific_term', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a WordPress plugin in a single PHP file. You can zip it and upload it like a normal plugin.