Last active
March 29, 2023 19:50
-
-
Save khleomix/476bdf5d9b7b6da1d378739b7188d5fd to your computer and use it in GitHub Desktop.
ACF Location rules for Multisite
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 | |
// Display specific ACF groups on selected sites only for Multisite. | |
if ( is_multisite() ) : | |
add_filter( 'acf/location/rule_types', 'acf_location_rule_type_multisite' ); | |
add_filter( 'acf/location/rule_values/site', 'acf_location_rule_values_multisites' ); | |
add_filter( 'acf/location/rule_match/site', 'acf_location_rules_match_site', 10, 3 ); | |
endif; | |
function acf_location_type_multisite( $choices ) { | |
$choices['Multisite']['site'] = 'Site'; | |
return $choices; | |
} | |
function acf_location_values_multisites( $choices ) { | |
$choices ['all'] = 'All'; | |
$sites = get_sites(); | |
foreach( $sites as $site ) { | |
$choices[ get_object_vars( $site )['blog_id'] ] = get_object_vars( $site )['path']; | |
} | |
// if on subdomains. | |
//foreach( $sites as $site ) { | |
// | |
// $name = get_blog_details( [ 'blog_id' => $site->blog_id ] )->blogname; | |
// | |
// $choices[ get_object_vars( $site )['blog_id'] ] = $name; | |
//} | |
return $choices; | |
} | |
function acf_location_rules_match_site( $match, $rule, $options ) { | |
$current_site = get_current_blog_id(); | |
$selected_site = (int) $rule['value']; | |
if ( '==' === $rule['operator'] ) : | |
$match = ( $current_site == $selected_site ); | |
elseif ( '!=' === $rule['operator'] ) : | |
$match = ( $current_site !== $selected_site ); | |
endif; | |
return $match; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment