get_option()

最后更新于:2021-11-26 10:37:00

get_option( string$option, mixed$default=false)

Retrieves an option value based on an option name.

参数

$option

(string) (Required) Name of the option to retrieve. Expected to not be SQL-escaped.

$default

(mixed) (Optional) Default value to return if the option does not exist.

Default value: false

响应

(mixed) Value of the option. A value of any type may be returned, including scalar (string, boolean, float, integer), null, array, object. Scalar and null values will be returned as strings as long as they originate from a database stored option value. If there is no option in the database, boolean false is returned.

源文件

文件: gc-includes/option.php

function get_option( $option, $default = false ) {
	global $gcdb;

	$option = trim( $option );
	if ( empty( $option ) ) {
		return false;
	}

	/*
	 * Until a proper _deprecated_option() function can be introduced,
	 * redirect requests to deprecated keys to the new, correct ones.
	 */
	$deprecated_keys = array(
		'blacklist_keys'    => 'disallowed_keys',
		'comment_whitelist' => 'comment_previously_approved',
	);

	if ( ! gc_installing() && isset( $deprecated_keys[ $option ] ) ) {
		_deprecated_argument(
			__FUNCTION__,
			'5.5.0',
			sprintf(
				/* translators: 1: Deprecated option key, 2: New option key. */
				__( 'The "%1$s" option key has been renamed to "%2$s".' ),
				$option,
				$deprecated_keys[ $option ]
			)
		);
		return get_option( $deprecated_keys[ $option ], $default );
	}

	/**
	 * Filters the value of an existing option before it is retrieved.
	 *
	 * The dynamic portion of the hook name, `$option`, refers to the option name.
	 *
	 * 响应ing a truthy value from the filter will effectively short-circuit retrieval
	 * and return the passed value instead.
	 *
	 * @since 1.5.0
	 * @since 4.4.0 The `$option` parameter was added.
	 * @since 4.9.0 The `$default` parameter was added.
	 *
	 * @param mixed  $pre_option The value to return instead of the option value. This differs
	 *                           from `$default`, which is used as the fallback value in the event
	 *                           the option doesn't exist elsewhere in get_option().
	 *                           Default false (to skip past the short-circuit).
	 * @param string $option     Option name.
	 * @param mixed  $default    The fallback value to return if the option does not exist.
	 *                           Default false.
	 */
	$pre = apply_filters( "pre_option_{$option}", false, $option, $default );

	if ( false !== $pre ) {
		return $pre;
	}

	if ( defined( 'GC_SETUP_CONFIG' ) ) {
		return false;
	}

	// Distinguish between `false` as a default, and not passing one.
	$passed_default = func_num_args() > 1;

	if ( ! gc_installing() ) {
		// Prevent non-existent options from triggering multiple queries.
		$notoptions = gc_cache_get( 'notoptions', 'options' );

		if ( isset( $notoptions[ $option ] ) ) {
			/**
			 * Filters the default value for an option.
			 *
			 * The dynamic portion of the hook name, `$option`, refers to the option name.
			 *
			 * @since 3.4.0
			 * @since 4.4.0 The `$option` parameter was added.
			 * @since 4.7.0 The `$passed_default` parameter was added to distinguish between a `false` value and the default parameter value.
			 *
			 * @param mixed  $default The default value to return if the option does not exist
			 *                        in the database.
			 * @param string $option  Option name.
			 * @param bool   $passed_default Was `get_option()` passed a default value?
			 */
			return apply_filters( "default_option_{$option}", $default, $option, $passed_default );
		}

		$alloptions = gc_load_alloptions();

		if ( isset( $alloptions[ $option ] ) ) {
			$value = $alloptions[ $option ];
		} else {
			$value = gc_cache_get( $option, 'options' );

			if ( false === $value ) {
				$row = $gcdb->get_row( $gcdb->prepare( "SELECT option_value FROM $gcdb->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;
					gc_cache_add( $option, $value, 'options' );
				} else { // Option does not exist, so we must cache its non-existence.
					if ( ! is_array( $notoptions ) ) {
						$notoptions = array();
					}

					$notoptions[ $option ] = true;
					gc_cache_set( 'notoptions', $notoptions, 'options' );

					/** This filter is documented in gc-includes/option.php */
					return apply_filters( "default_option_{$option}", $default, $option, $passed_default );
				}
			}
		}
	} else {
		$suppress = $gcdb->suppress_errors();
		$row      = $gcdb->get_row( $gcdb->prepare( "SELECT option_value FROM $gcdb->options WHERE option_name = %s LIMIT 1", $option ) );
		$gcdb->suppress_errors( $suppress );

		if ( is_object( $row ) ) {
			$value = $row->option_value;
		} else {
			/** This filter is documented in gc-includes/option.php */
			return apply_filters( "default_option_{$option}", $default, $option, $passed_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' ), true ) ) {
		$value = untrailingslashit( $value );
	}

	/**
	 * Filters the value of an existing option.
	 *
	 * The dynamic portion of the hook name, `$option`, refers to the option name.
	 *
	 * @since 1.5.0 As 'option_' . $setting
	 * @since 3.0.0
	 * @since 4.4.0 The `$option` parameter was added.
	 *
	 * @param mixed  $value  Value of the option. If stored serialized, it will be
	 *                       unserialized prior to being returned.
	 * @param string $option Option name.
	 */
	return apply_filters( "option_{$option}", maybe_unserialize( $value ), $option );
}
/**
 * Retrieves multiple options values as an associative array based on an option name.
 * @param string $option_name (Required) Name of option to retrieve.
 * @param array $defaults (Optional) Default values to return if the option does not exist.
 */
function gcorg_prfex_get_theme_option( $option_name, $defaults = array() ) {
  $options = get_option( $option_name );
  //checking if options exists
  if ( ! empty( $options ) ) {
    $it = new MultipleIterator();
    $it->attachIterator( new ArrayIterator( $options ) );
    $it->attachIterator( new ArrayIterator( $defaults ) );

    foreach ( $it as $setting_name => $value ) {
      if ( empty( $value[0] ) ) {
        $optionsArray[ $setting_name[1] ] = $value[1];
      } else {
        $optionsArray[ $setting_name[0] ] = $value[0];
      }
    }// end loop
    return $optionsArray;
  } else {
    // if no options then set the defaults
    $optionsArray = $defaults;
    return $optionsArray;
  }
}