get_blog_option

Definition:
function get_blog_option( $blog_id, $setting, $default = false ) {}

Retrieve option value based on setting name and blog_id.
If the option does not exist or does not have a value, then the return value will be false. This is useful to check whether you need to install an option and is commonly used during installation of plugin options and to test whether upgrading is required.

Parameters

  • int $blog_id: Optional. Blog ID, can be null to refer to the current blog.
  • string $setting: Name of option to retrieve. Should already be SQL-escaped.
  • string $default: (optional) Default value returned if option not found.

Return values

returns:Value set for the option.

Defined filters

  • blog_option_$setting
    apply_filters( 'blog_option_' . $setting, $value, $blog_id )

Source code

function get_blog_option( $blog_id, $setting, $default = false ) {

	global $wpdb;



	if ( null === $blog_id )

		$blog_id = $wpdb->blogid;



	$key = $blog_id . '-' . $setting . '-blog_option';

	$value = wp_cache_get( $key, 'site-options' );

	if ( $value == null ) {

		if ( $blog_id == $wpdb->blogid ) {

			$value = get_option( $setting, $default );

			$notoptions = wp_cache_get( 'notoptions', 'options' );

			if ( isset( $notoptions[$setting] ) ) {

				wp_cache_set( $key, 'noop', 'site-options' );

				$value = $default;

			} elseif ( $value == false ) {

				wp_cache_set( $key, 'falsevalue', 'site-options' );

			} else {

				wp_cache_set( $key, $value, 'site-options' );

			}

			return apply_filters( 'blog_option_' . $setting, $value, $blog_id );

		} else {

			$blog_prefix = $wpdb->get_blog_prefix( $blog_id );

			$row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$blog_prefix}options WHERE option_name = %s", $setting ) );

			if ( is_object( $row ) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values

				$value = $row->option_value;

				if ( $value == false )

					wp_cache_set( $key, 'falsevalue', 'site-options' );

				else

					wp_cache_set( $key, $value, 'site-options' );

			} else { // option does not exist, so we must cache its non-existence

				wp_cache_set( $key, 'noop', 'site-options' );

				$value = $default;

			}

		}

	} elseif ( $value == 'noop' ) {

1214

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: