関数 get_option()
2014/11/25
定義ファイル :/blog/wp-includes/option.php
呼出し元 :/blog/wp-includes/compat.php
optionsデータベーステーブルから、指定したオプションの値を取得する
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | <?php /** * Option API * * @package WordPress * @subpackage Option */ /** * 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. * * If the option was serialized then it will be unserialized when it is returned. * * @since 1.5.0 * * @param string $option Name of option to retrieve. Expected to not be SQL-escaped. * @param mixed $default Optional. Default value to return if the option does not exist. * @return mixed Value set for the option. */ function get_option( $option, $default = false ) { global $wpdb; $option = trim( $option ); if ( empty( $option ) ) return false; /** * Filter the value of an existing option before it is retrieved. * * The dynamic portion of the hook name, $option, refers to the option name. * * Passing a truthy value to the filter will short-circuit retrieving * the option value, returning the passed value instead. * * @since 1.5.0 * * @param bool|mixed $pre_option Value to return instead of the option value. * Default false to skip it. */ $pre = apply_filters( 'pre_option_' . $option, false ); if ( false !== $pre ) return $pre; 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] ) ) /** * Filter the default value for an option. * * The dynamic portion of the hook name, $option, refers * to the option name. * * @since 3.4.0 * * @param mixed $default The default value to return if the option * does not exist in the database. */ return apply_filters( 'default_option_' . $option, $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' ); /** This filter is documented in wp-includes/option.php */ return apply_filters( 'default_option_' . $option, $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 { /** This filter is documented in wp-includes/option.php */ return apply_filters( 'default_option_' . $option, $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 ); /** * Filter 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 * * @param mixed $value Value of the option. If stored serialized, it will be * unserialized prior to being returned. */ return apply_filters( 'option_' . $option, maybe_unserialize( $value ) ); } |
Codex
get_option() optionsデータベーステーブルから、指定したオプションの値を取得する安全な方法です。希望するオプションが存在しない場合は、値が関連付けされず、FALSE が返されます。