Plugin settings only avail. in blog #1 – get_option() solutions?
-
Hi,
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; else 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 ) ); }
- The topic ‘Plugin settings only avail. in blog #1 – get_option() solutions?’ is closed to new replies.