Using WordPress ‘get_option()’ PHP function

The get_option() WordPress PHP function retrieves an option value based on an option name.

Usage

$value = get_option('option_name', 'default_value');

Parameters

  • $option (string): Name of the option to retrieve. Expected to not be SQL-escaped.
  • $default_value (mixed): Optional. Default value to return if the option does not exist. Default: false.

More information

See WordPress Developer Resources: get_option()

Examples

Retrieve Blog Title

Display your blog’s title in a <h1> tag:

echo '<h1>' . get_option('blogname') . '</h1>';

Retrieve Administrator Email

Retrieve the email of the blog administrator and store it in a variable:

$admin_email = get_option('admin_email');

Retrieve Character Set

Display the character set your blog is using (e.g., UTF-8):

echo '<p>' . esc_html(sprintf(__('Character set: %s', 'textdomain'), get_option('blog_charset'))) . '</p>';

Retrieve Active Plugins

Retrieve an array of all active plugins:

$active_plugins = get_option('active_plugins');

Check if Option is Set

Check if an option is set to avoid warnings when using a checkbox on a plugin’s options page:

function comment_author_url_render() {
    $options = get_option('plugin_settings');
    echo '<input type="checkbox" name="plugin_settings[setting]"' . checked(isset($options['setting'])) . ' value="1">';
}