Support

Account

Home Forums Backend Issues (wp-admin) Multisite – Location Rules + Display field group(s) on specific sites

Solving

Multisite – Location Rules + Display field group(s) on specific sites

  • This may help others who have large multisite environments and want certain field groups to display on one or more. It’s initial scope is limited to single-site selection, but could certainly be expanded to pick up site option info or other differentiating details of the sites.

    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);
    		}
    
    function acf_location_rule_type_multisite( $choices ) {
    	
    		$choices['Multisite']['site'] = 'Site';
    		return $choices;
    		
    	}
    
    	function acf_location_rule_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"];
    		}
    						
    		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 );
        	}
    
        	return $match;
    	}
    

    Including it as a part of an mu-plugin is highly suggested. I found it particularly useful for User Meta in a multisite case where a common set of meta is updated from a 3rd party system and only want the main site to have the group info.

  • works great, but for me I had to change

    $choices[ get_object_vars($site)[“blog_id”] ] = get_object_vars($site)[“path”];

    to
    $choices[ get_object_vars($site)[“blog_id”] ] = get_object_vars($site)[“domain”];

    since it did not return anything. But nonetheless great add-on!

  • Thank you for the great script. That’s exactly what I was looking for. How do I get the fields in the front? This only works on the main page where the options page is also available. unfortunately, nothing is shown on the other pages. Can you help me? Thank you in advance

    the_field( ‘telefon_opt’, ‘option’ );

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Multisite – Location Rules + Display field group(s) on specific sites’ is closed to new replies.