wp-includes/option.php:78Read a saved setting from the options table with get_option(), which returns your $default_value, or false, when the option was never saved. Serialized arrays and objects come back with their original type, but scalar values stored in the database are returned as strings, so avoid strict comparisons against booleans or numbers.
Retrieves an option value based on an option name.
$optionstring$default_valuemixedoptionalfalsemixedEvery example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Fetch a settings array and supply a default so the code works before the option is ever saved.
$settings = get_option( 'myplugin_settings', array() );
$color = $settings['accent_color'] ?? '#0055aa';
printf(
'<div class="promo" style="--accent: %s">%s</div>',
esc_attr( $color ),
esc_html__( 'Sale on now', 'myplugin' )
);A stored false and a missing option both come back as false unless you pass a distinct $default_value; use a sentinel default when you need to tell them apart.
Pull built-in settings such as the site title and posts-per-page count.
$site_name = get_option( 'blogname' );
$per_page = (int) get_option( 'posts_per_page' );
printf(
'<p>%s shows %d posts per page.</p>',
esc_html( $site_name ),
$per_page
);Database-stored scalars are returned as strings ('1' for true, '0' for 0), so cast before doing arithmetic or strict comparisons; repeatedly reading an option that was never saved also costs an extra query, which add_option() with a default avoids.
6 hooks fire while get_option() runs, in this order:
Filters the value of an existing option before it is retrieved.
Filters the value of all existing options before it is retrieved.
Filters the default value for an option.
Filters the default value for an option.
Filters the default value for an option.
Filters the value of an existing option.
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;Introduced in 1.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
src/wp-includes/option.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.