-
-
Save dgoze/95310ee1449b4632ca13cb6c1cce5c78 to your computer and use it in GitHub Desktop.
WordPress ACF Multisite location rule
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 | |
/** | |
* | |
* Adds location rule to target specific sites in a WordPress Multisite environment | |
* http://www.advancedcustomfields.com/resources/custom-location-rules/ | |
* | |
*/ | |
function acf_location_rule_type_blog( $choices ) { | |
$choices['Basic']['blog'] = 'Multisite Blog'; | |
return $choices; | |
} | |
add_filter('acf/location/rule_types', 'acf_location_rule_type_blog'); | |
function acf_location_rules_values_blog( $choices ) { | |
$sites = wp_get_sites(); | |
$choices = array(); | |
foreach ($sites as $key => $value) { | |
$blog_details = get_blog_details($value['blog_id']); | |
$choices[$value['blog_id']] = $blog_details->blogname; | |
} | |
return $choices; | |
} | |
add_filter('acf/location/rule_values/blog', 'acf_location_rules_values_blog'); | |
function acf_location_rules_match_blog( $match, $rule, $options ) { | |
$current_blog = get_current_blog_id(); | |
$selected_blog = (int) $rule['value']; | |
if ($rule['operator'] == "==") { | |
$match = ( $current_blog == $selected_blog ); | |
} elseif ($rule['operator'] == "!=") { | |
$match = ( $current_blog != $selected_blog ); | |
} | |
return $match; | |
} | |
add_filter('acf/location/rule_match/blog', 'acf_location_rules_match_blog', 10, 3); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment