Plugin settings only avail. in blog #1 - get_option() solutions? (3 posts)

  1. beatbox433
    Posted 5 years ago #

    For a site I'm setting up (w/ bits of Buddypress) I do not make the plugins page available to users. Typically I want the settings for plugins to be set and read only from the super admin's blog, blog #1.

    Often plugins will call get_option(). I am wary of using switch_to_blog(1) before calling this, as it is frequently called, and I'm worried about performance.
    Other options might be to copy the plugin options to every single blog in the system, and update them when the options settings in blog#1 are updated. (Yuk.)
    I have also considered making an alternative get_option() function and just hardcoding the db queries to blog#1. Something like the following, which is just based on get_option. I guess I need to lose the cache stuff. Any other potential hazards? Or other solutions?

    function myplugin_get_option( $option, $default = false ) {
    	global $wpdb;
    	// Allow plugins to short-circuit options.
    	$pre = apply_filters( 'pre_option_' . $option, false );
    	if ( false !== $pre )
    		return $pre;
    	$option = trim($option);
    	if ( empty($option) )
    		return false;
    	if ( defined( 'WP_SETUP_CONFIG' ) )
    		return false;
    	if ( ! defined( 'WP_INSTALLING' ) ) {
    		// prevent non-existent options from triggering multiple queries
    		$notoptions = wp_cache_get( 'notoptions', 'options' );
    		if ( isset( $notoptions[$option] ) )
    			return $default;
    		$alloptions = wp_load_alloptions();
    		if ( isset( $alloptions[$option] ) ) {
    			$value = $alloptions[$option];
    		} else {
    			$value = wp_cache_get( $option, 'options' );
    			if ( false === $value ) {
    				$row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM wp_options WHERE option_name = %s LIMIT 1", $option ) );
    				// Has to be get_row instead of get_var because of funkiness with 0, false, null values
    				if ( is_object( $row ) ) {
    					$value = $row->option_value;
    					wp_cache_add( $option, $value, 'options' );
    				} else { // option does not exist, so we must cache its non-existence
    					$notoptions[$option] = true;
    					wp_cache_set( 'notoptions', $notoptions, 'options' );
    					return $default;
    	} else {
    		$suppress = $wpdb->suppress_errors();
    		$row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM wp_options WHERE option_name = %s LIMIT 1", $option ) );
    		$wpdb->suppress_errors( $suppress );
    		if ( is_object( $row ) )
    			$value = $row->option_value;
    			return $default;
    	// If home is not set use siteurl.
    	if ( 'home' == $option && '' == $value )
    		return get_option( 'siteurl' );
    	if ( in_array( $option, array('siteurl', 'home', 'category_base', 'tag_base') ) )
    		$value = untrailingslashit( $value );
    	return apply_filters( 'option_' . $option, maybe_unserialize( $value ) );
  2. David Sader
    Posted 5 years ago #

    Is what you are looking to do going to get done by simply hooking into the pre_option filter?

    Something like this could work to override an option on a given blog if activated from "plugins" or override the option in all blogs if network activated or dropped into "mu-plugins":

    function override_foo() {
    	$bar = 'Snarfer'; // the value of the option
        return $bar;
    $option = 'blogname'; // whatever blog option you like
    add_filter( 'pre_option_' . $option, 'override_foo' );

    This way may save you time down the road when a plugin updates from the repo and you haven't actually altered it to hardcode your defaults.

  3. beatbox433
    Posted 5 years ago #

    That sounds like a good way to go. I'll look into it. I'm not so up to speed on the different hooks available.

Topic Closed

This topic has been closed to new replies.

About this Topic