Advertisements

get_option

Definition:
function get_option( $option, $default = false ) {}

Retrieve option value based on name of option.
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

  • string $option: Name of option to retrieve. Expected to not be SQL-escaped.
  • mixed $default: Optional. Default value to return if the option does not exist.

Return values

returns:Value set for the option.

Defined filters

  • pre_option_$option
    apply_filters( 'pre_option_' . $option, false )
  • option_$option
    apply_filters( 'option_' . $option, maybe_unserialize( $value )

Source code

function 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 $wpdb->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 $wpdb->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 ) );

}

1514

Advertisements

Trackbacks/Pingbacks

  1. get_option で、存在しないデータを読んだときの戻り値は false | X->A->O - June 10, 2011

    […] 参考にしたのがこれ→https://hitchhackerguide.com/2011/02/12/get_option/ […]

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 )

Google+ photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s

%d bloggers like this: