Created
May 2, 2017 06:42
-
-
Save nickdavis/09beef32aab86e4badf57fe55ef0831d to your computer and use it in GitHub Desktop.
(No longer) used Advanced Custom Field custom location rules for a client project, that might be useful for something else in future (props: Bill Erickson, as usual)
This file contains 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 | |
/** | |
* Register conditional locaton rules for Advanced Custom Fields | |
* | |
* @package ${NAMESPACE}. | |
* @since 1.0.0 | |
* @author iamnickdavis | |
* @link http://iamnickdavis.com | |
* @license GNU General Public License 2.0+ | |
*/ | |
add_filter( 'acf/location/rule_types', __NAMESPACE__ . '\register_rule_type_has_locations' ); | |
/** | |
* ACF Rule Type: Has Locations | |
* | |
* @author Bill Erickson | |
* @see http://www.billerickson.net/acf-custom-location-rules | |
* | |
* @param array $choices , all of the available rule types | |
* | |
* @return array | |
*/ | |
function ta_acf_rule_type_has_locations( $choices ) { | |
$choices['Post']['has_locations'] = 'Has Locations'; | |
return $choices; | |
} | |
add_filter( 'acf/location/rule_values/has_locations', 'ta_acf_rule_values_has_locations' ); | |
/** | |
* ACF Rule Values: Has Locations | |
* | |
* @author Bill Erickson | |
* @see http://www.billerickson.net/acf-custom-location-rules | |
* | |
* @param array $choices , available rule values for this type | |
* | |
* @return array | |
*/ | |
function ta_acf_rule_values_has_locations( $choices ) { | |
$choices = [ 'True' ]; | |
return $choices; | |
} | |
add_filter( 'acf/location/rule_match/has_locations', 'ta_acf_rule_match_has_locations', 10, 3 ); | |
/** | |
* ACF Rule Match: Has Locations | |
* | |
* @author Bill Erickson | |
* @see http://www.billerickson.net/acf-custom-location-rules | |
* | |
* @param boolean $match , whether the rule matches (true/false) | |
* @param array $rule , the current rule you're matching. Includes 'param', 'operator' and 'value' parameters | |
* @param array $options , data about the current edit screen (post_id, page_template...) | |
* | |
* @return boolean $match | |
*/ | |
function ta_acf_rule_match_has_locations( $match, $rule, $options ) { | |
// Only run for 'has_locations' value | |
if ( 'has_locations' != $rule['param'] ) { | |
return $match; | |
} | |
// Only run if post ID is defined and this is a provider | |
if ( ! $options['post_id'] || 'provider' != get_post_type( $options['post_id'] ) ) { | |
return $match; | |
} | |
$terms = get_the_terms( $options['post_id'], 'location' ); | |
$count = count( $terms ); | |
if ( $count > 1 ) { | |
$match = 'True'; | |
} else { | |
return $match; | |
} | |
return $match; | |
} | |
add_filter( 'acf/location/rule_types', 'ta_acf_rule_type_has_specialty' ); | |
/** | |
* ACF Rule Type: Has Specialties | |
* | |
* @author Bill Erickson | |
* @see http://www.billerickson.net/acf-custom-location-rules | |
* | |
* @param array $choices , all of the available rule types | |
* | |
* @return array | |
*/ | |
function ta_acf_rule_type_has_specialty( $choices ) { | |
$choices['Post']['has_specialty'] = 'Has Specialties'; | |
return $choices; | |
} | |
add_filter( 'acf/location/rule_values/has_specialty', 'ta_acf_rule_values_has_specialty' ); | |
/** | |
* ACF Rule Values: Has Specialties | |
* | |
* @author Bill Erickson | |
* @see http://www.billerickson.net/acf-custom-location-rules | |
* | |
* @param array $choices , available rule values for this type | |
* | |
* @return array | |
*/ | |
function ta_acf_rule_values_has_specialty( $choices ) { | |
$choices = [ 'True' ]; | |
return $choices; | |
} | |
add_filter( 'acf/location/rule_match/has_specialty', 'ta_acf_rule_match_has_specialty', 10, 3 ); | |
/** | |
* ACF Rule Match: Has Specialties | |
* | |
* @author Bill Erickson | |
* @see http://www.billerickson.net/acf-custom-location-rules | |
* | |
* @param boolean $match , whether the rule matches (true/false) | |
* @param array $rule , the current rule you're matching. Includes 'param', 'operator' and 'value' parameters | |
* @param array $options , data about the current edit screen (post_id, page_template...) | |
* | |
* @return boolean $match | |
*/ | |
function ta_acf_rule_match_has_specialty( $match, $rule, $options ) { | |
// Only run for 'has_specialty' value | |
if ( 'has_specialty' != $rule['param'] ) { | |
return $match; | |
} | |
// Only run if post ID is defined and this is a provider | |
if ( ! $options['post_id'] || 'provider' != get_post_type( $options['post_id'] ) ) { | |
return $match; | |
} | |
$terms = get_the_terms( $options['post_id'], 'specialty' ); | |
$count = count( $terms ); | |
if ( $count > 1 ) { | |
$match = 'True'; | |
} else { | |
return $match; | |
} | |
return $match; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment