WordPressを読む 11-1 /blog/wp-includes/option.php 1
2014/11/29
目次
/blog/wp-includes/option.php 1
読込元 : /blog/wp-includes/functions.php 1
関数
get_option()
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 ) ); } |
関数
wp_protect_special_option()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * Protect WordPress special option from being modified. * * Will die if $option is in protected list. Protected options are 'alloptions' * and 'notoptions' options. * * @since 2.2.0 * * @param string $option Option name. */ function wp_protect_special_option( $option ) { if ( 'alloptions' === $option || 'notoptions' === $option ) wp_die( sprintf( __( '%s is a protected WP option and may not be modified' ), esc_html( $option ) ) ); } |
関数
form_option()
1 2 3 4 5 6 7 8 9 10 11 | /** * Print option value after sanitizing for forms. * * @uses attr Sanitizes value. * @since 1.5.0 * * @param string $option Option name. */ function form_option( $option ) { echo esc_attr( get_option( $option ) ); } |
関数
wp_load_alloptions()
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 | /** * Loads and caches all autoloaded options, if available or all options. * * @since 2.2.0 * * @return array List of all options. */ function wp_load_alloptions() { global $wpdb; if ( !defined( 'WP_INSTALLING' ) || !is_multisite() ) $alloptions = wp_cache_get( 'alloptions', 'options' ); else $alloptions = false; if ( !$alloptions ) { $suppress = $wpdb->suppress_errors(); if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) ) $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" ); $wpdb->suppress_errors($suppress); $alloptions = array(); foreach ( (array) $alloptions_db as $o ) { $alloptions[$o->option_name] = $o->option_value; } if ( !defined( 'WP_INSTALLING' ) || !is_multisite() ) wp_cache_add( 'alloptions', $alloptions, 'options' ); } return $alloptions; } |