Skip to content

Instantly share code, notes, and snippets.

@martinsanne
Created January 30, 2015 14:51
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save martinsanne/72f48bad22d99032ba0e to your computer and use it in GitHub Desktop.
Save martinsanne/72f48bad22d99032ba0e to your computer and use it in GitHub Desktop.
WordPress ACF Multisite location rule
<?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);
?>
@micheldenegri
Copy link

This is awesome!, thank you for sharing.

@jonnymaceachern
Copy link

Updated here for 4.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment