Support

Account

Home Forums Feature Requests Support Multisite options for all sites through sitemeta table

Solving

Support Multisite options for all sites through sitemeta table

  • This feature request is about adding an ACF Options page to save settings on a multi-site installation that applies to all sites on the network, using the wp_sitemeta table instead of wp_{id}_options.

    Example scenario:

    Imagine a multisite installation, where each site of the network belongs to an Affiliate:
    – Affiliate A
    – Affiliate B

    There’s an “Affiliate Settings” options page, with options such as site logo and colors, where each Affiliate can change for their own site.

    Now, imagine that I want another options page, with global values for all Affiliates, such as Google Tag Manager ID, Social Media links, etc. I call it “General Settings” and restrict it only for the super-admin role. The super-admin modifies those options, which is stored on wp_sitemeta table, which applies to all sites on the network.

    I currently can’t do that. Currently, with ACF, I can only store meta on the wp_{id}_options table, which stores the settings for each site separately.

  • I have managed to accomplish network-level settings with some filters:

    
    // When loading the value for the "Foo" field, get it from the sitemeta table:
    add_filter( "acf/load_value/key=foo", function () use ( $field_key ) {
    	$value = get_site_option( $field_key );
    
    	if ( $value !== false ) {
    		return $value;
    	}
    
    	return '';
    } );
    
    // When setting the value for the "Foo" field, set it to sitemeta table:
    add_filter( "acf/update_value/key=foo", function ( $value ) use ( $field_key ) {
    	return add_site_option( $field_key, $value );
    } );
    

    Result: Updating the field “Foo” in any of the network sites applies it to all of the network sites.

    It’s up to the developer, though, to make sure that these fields are assigned to an Options page which has the super-admin permission callback.

  • Seems I can’t edit my answer after some time. These are the filters I ended up using:

    
    $field_key = 'foo';
    
    add_filter( "acf/load_value/key=$field_key", function ( $original_value ) use ( $field_key ) {
        $network_value = get_site_option( $field_key, null );
    
        if ( ! is_null( $network_value ) ) {
            // Return the value from the "sitemeta" table, if available
            return $network_value;
        } else {
            // This will only happen if the field was never updated.
            return $original_value;
        }
    
    } );
    
    add_filter( "acf/update_value/key=$field_key", function ( $value ) use ( $field_key ) {
        $old_network_value = get_site_option( $field_key, null );
    
        // Replicate the field value to the "sitemeta" table
        if ( is_null( $old_network_value ) ) {
            add_site_option( $field_key, $value );
        } else {
            update_site_option( $field_key, $value );
        }
    
        $new_network_value = get_site_option( $field_key );
    
        return $new_network_value;
    } );
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.