get_option()

get get_option( string $option, mixed $default_value = false ): mixed

Lấy giá trị của một tùy chọn trong cơ sở dữ liệu.

function get_option( $option, $default_value = false ) {
	global $wpdb;

	if ( is_scalar( $option ) ) {
		$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 ( isset( $deprecated_keys[ $option ] ) && ! wp_installing() ) {
		_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_value );
	}

	/**
	 * Filters the value of an existing option before it is retrieved.
	 *
	 * The dynamic portion of the hook name, `$option`, refers to the option name.
	 *
	 * Returning a value other than false from the filter will short-circuit retrieval
	 * and return that value instead.
	 *
	 * @since 1.5.0
	 * @since 4.4.0 The `$option` parameter was added.
	 * @since 4.9.0 The `$default_value` parameter was added.
	 *
	 * @param mixed  $pre_option    The value to return instead of the option value. This differs from
	 *                              `$default_value`, 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_value The fallback value to return if the option does not exist.
	 *                              Default false.
	 */
	$pre = apply_filters( "pre_option_{$option}", false, $option, $default_value );

	/**
	 * Filters the value of all existing options before it is retrieved.
	 *
	 * Returning a truthy value from the filter will effectively short-circuit retrieval
	 * and return the passed value instead.
	 *
	 * @since 6.1.0
	 *
	 * @param mixed  $pre_option    The value to return instead of the option value. This differs from
	 *                              `$default_value`, 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        Name of the option.
	 * @param mixed  $default_value The fallback value to return if the option does not exist.
	 *                              Default false.
	 */
	$pre = apply_filters( 'pre_option', $pre, $option, $default_value );

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

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

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

	if ( ! wp_installing() ) {
		$alloptions = wp_load_alloptions();
		/*
		 * When getting an option value, we check in the following order for performance:
		 *
		 * 1. Check the 'alloptions' cache first to prioritize existing loaded options.
		 * 2. Check the 'notoptions' cache before a cache lookup or DB hit.
		 * 3. Check the 'options' cache prior to a DB hit.
		 * 4. Check the DB for the option and cache it in either the 'options' or 'notoptions' cache.
		 */
		if ( isset( $alloptions[ $option ] ) ) {
			$value = $alloptions[ $option ];
		} else {
			// Check for non-existent options first to avoid unnecessary object cache lookups and DB hits.
			$notoptions = wp_cache_get( 'notoptions', 'options' );

			if ( ! is_array( $notoptions ) ) {
				$notoptions = array();
				wp_cache_set( 'notoptions', $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_value  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_value, $option, $passed_default );
			}

			$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_value, $option, $passed_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_value, $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 );
}

'admin_email' – Địa chỉ email của quản trị viên trang web.

'blogname' – Tiêu đề của trang web; được thiết lập trong Cài đặt Chung.

'blogdescription' – Phương châm (tagline) của trang web; được thiết lập trong Cài đặt Chung.

'blog_charset' – Mã hóa ký tự của trang web; được thiết lập trong Cài đặt Đọc.

'date_format' – Định dạng ngày mặc định; được thiết lập trong Cài đặt Chung.

'default_category' – Danh mục bài viết mặc định; được thiết lập trong Cài đặt Viết.

'home' – Địa chỉ trang chủ của trang web; được thiết lập trong Cài đặt Chung.

'posts_per_page' – Số bài viết tối đa hiển thị trên một trang; được thiết lập trong Cài đặt Đọc.

'posts_per_rss' – Số bài viết gần đây nhất tối đa hiển thị trong nguồn cấp dữ liệu; được thiết lập trong Cài đặt Đọc.

'siteurl' – Địa chỉ web của WordPress; được thiết lập trong Cài đặt Chung.

Lưu ý: Không giống như get_bloginfo('url') (trả về URL trang chủ), mà giống với get_bloginfo('wpurl').


'template' – Tên của giao diện (theme) hiện tại.

'start_of_week' – Ngày bắt đầu của tuần trong lịch; được thiết lập trong Cài đặt Chung.

'upload_path' – Đường dẫn tải lên mặc định; được thiết lập trong Cài đặt Thư viện.

'users_can_register' – Cho phép người dùng đăng ký hay không; được thiết lập trong Cài đặt Chung.

Ví dụ 1: Ngày bắt đầu của tuần trong lịch; được thiết lập trong Cài đặt Chung.
echo get_option('start_of_week');
[Monday]
Ví dụ 2: Ngày bắt đầu của tuần trong lịch; được thiết lập trong Cài đặt Chung.
echo get_option('start_of_week');
[Monday]
Ví dụ 3: Tên của giao diện (theme) hiện tại.
echo get_option('template');
[ten-giao-dien]
Ví dụ 4: Tên của giao diện (theme) hiện tại.
echo get_option('template');
[ten-giao-dien]
Ví dụ 5: Cho phép người dùng đăng ký hay không; được thiết lập trong Cài đặt Chung.
echo get_option('users_can_register');
true or false
Ví dụ 6: Địa chỉ web của WordPress; được thiết lập trong Cài đặt Chung.
echo get_option('siteurl');
http://localhost
Ví dụ 7: Số bài viết gần đây nhất tối đa hiển thị trong nguồn cấp dữ liệu; được thiết lập trong Cài đặt Đọc.
echo get_option('posts_per_rss');
10
Ví dụ 8: Số bài viết tối đa hiển thị trên một trang; được thiết lập trong Cài đặt Đọc.
echo get_option('posts_per_page');
10
Ví dụ 9: Địa chỉ trang chủ của trang web; được thiết lập trong Cài đặt Chung.
echo get_option('home');
http://localhost
Ví dụ 10: Danh mục bài viết mặc định; được thiết lập trong Cài đặt Viết.
echo get_option('default_category');
category
Ví dụ 11: Định dạng ngày mặc định; được thiết lập trong Cài đặt Chung.
echo get_option('date_format');
d-M-Y
Ví dụ 12: Mã hóa ký tự của trang web; được thiết lập trong Cài đặt Đọc.
echo get_option('blog_charset');
UTF-8
Ví dụ 13: Phương châm (tagline) của trang web; được thiết lập trong Cài đặt Chung.
echo get_option('blogdescription');
Mô tả web
Ví dụ 14: Tiêu đề của trang web; được thiết lập trong Cài đặt Chung.
echo get_option('blogname');
Tên trang web
Ví dụ 15: Lấy địa chỉ email của quản trị viên trang web.
echo get_option('admin_email');
Email admin website