Function Reference/get option

来自WordPress中文文档
跳转至: 导航搜索

wordpress.org.cn

Description

A safe way of getting values for a named option from the options database table. If the desired option does not exist, or no value is associated with it, FALSE will be returned.

Usage

%%% <?php echo get_option( $option, $default ); ?> %%%

Parameters

tt$option/tt
(string) (required) Name of the option to retrieve. A concise list of valid options is below, but a more complete one can be found at the Option Reference. Matches $option_name in register_setting() for custom options.
Default: Nonenoinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

  • 'admin_email' - E-mail address of blog administrator.
  • 'blogname' - Weblog title; set in General Options.
  • 'blogdescription' - Tagline for your blog; set in General Options.
  • 'blog_charset' - Character encoding for your blog; set in Reading Options.
  • 'date_format' - Default date format; set in General Options.
  • 'default_category' - Default post category; set in Writing Options.
  • 'home' - The blog's home web address; set in General Options.
  • 'siteurl' - WordPress web address; set in General Options.
    Warning: This is not the same as get_bloginfo('siteurl') (which will return the homepage url), but as get_bloginfo('wpurl').
  • 'template' - The current theme's name; set in Presentation.
  • 'start_of_week' - Day of week calendar should start on; set in General Options.
  • 'upload_path' - Default upload location; set in Miscellaneous Options.
  • 'posts_per_page' - Maximum number of posts to show on a page; set in Reading Options.
  • 'posts_per_rss' - Maximum number of most recent posts to show in the syndication feed; set in Reading Options.
There are many more options available, a lot of which depend on what plugins you have installed. Visit the /wp-admin/options.php page for a complete list.

Underscores separate words, lowercase only - this is going to be in a database.

tt$default/tt
(mixed) (optional) The default value to return if no value is returned (ie. the option is not in the database).
Default: falsenoinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

Return Values

Mixed values for the option. If the option does not exist, returns boolean FALSE.

Examples

%%%<?php

$no_exists_value = get_option( 'no_exists_value' ); var_dump( $no_exists_value ); /* outputs false */

$no_exists_value = get_option( 'no_exists_value', 'default_value' ); var_dump( $no_exists_value ); /* outputs 'default_value' */

?>%%%

Show Blog Title

Displays your blog's title in a <h1> tag.

%%%

<?php echo get_option( 'blogname' ); ?>

 %%%

Show Character Set

Displays the character set your blog is using (ex: UTF-8)

%%%

Character set: <?php echo get_option( 'blog_charset' ); ?>

 %%%

Retrieve Administrator E-mail

Retrieve the e-mail of the blog administrator, storing it in a variable. %%% <?php $admin_email = get_option( 'admin_email' ); ?> %%%

Notes

  • Uses: apply_filters()
    • Calls 'pre_option_$option' before checking the option. Any value other than false will "short-circuit" the retrieval of the option and return the returned value. You should not try to override special options, but you will not be prevented from doing so.
    • Calls 'option_$option', after checking the option, with the option value.
    • Calls 'default_option_$option' to filter $default before returning it if it is set and the value doesn't exist.
  • Uses: maybe_unserialize() to unserialize the value before returning it.

Changelog

  • Since 1.5.0

Source File

get_option() is located in onlyincludecodewp-includes/option.php/code/onlyinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Template Description

Link to the source code on http://core.trac.wordpress.org/browser/.

Parameters

  1. filename
  2. (option) path to codetag/code (version) or codetrunk/code. This option is only used for a new function.br /Default: codetrunk/code -- trunk is the latest bleeding edge development version of WordPress.

Usage

Link to the stable version: pre检查到模板循环:模板:Trac/pre

Link to trunk: pre检查到模板循环:模板:Trac/pre

/div

wordpress.org.cn.

Related

Options API: get_option, add_option, update_option, delete_option

includeonlydiv style=clear:both; background-color:#F7F7F7; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;See also index of Function Reference and index of Template Tags./div/includeonlynoinclude

Description

This Template is used by Codex:Template Messages.

Usage

pre 检查到模板循环:模板:Message /pre

Result

检查到模板循环:模板:Message

/noinclude