SponsoredHosinger Special Offer

WP+ WordPress Plugin Development

Build WordPress plugins from scratch: actions and filters, shortcodes, settings pages, custom post types, meta boxes, AJAX, REST endpoints, custom tables, cron and blocks.

The Options API

WordPress Plugin Development Lesson 11 of 27 ~2 min read

Overview

The Options API stores site-wide values in the wp_options table under a unique name, and its four functions — add_option(), get_option(), update_option() and delete_option() — are the simplest way for a plugin to remember settings and small pieces of state.

The four functions

<?php
// Create, only if it does not exist yet.
add_option( 'gdc_books_installed_at', time() );

// Read, with a default for when it is missing.
$settings = get_option( 'gdc_books_settings', array() );

// Create or overwrite.
update_option( 'gdc_books_last_import', current_time( 'mysql' ) );

// Remove.
delete_option( 'gdc_books_last_import' );

Options store any PHP value — strings, numbers, arrays, objects. WordPress serialises them automatically, so arrays come back as arrays.

Reading array options safely

An option saved by an older version of your plugin may be missing keys you added later. Merge with defaults every time you read it:

<?php
function gdc_books_get_settings() {
    $defaults = array(
        'books_per_page' => 6,
        'show_isbn'      => 1,
        'currency'       => 'USD',
    );

    return wp_parse_args( get_option( 'gdc_books_settings', array() ), $defaults );
}

// Everywhere else in the plugin:
$per_page = gdc_books_get_settings()['books_per_page'];

Autoload: the performance setting most plugins get wrong

On every request, WordPress loads all autoloaded options in a single query and keeps them in memory. That makes get_option() instant for them — but every autoloaded option costs memory on every page, even pages that never use it.

<?php
// Needed on most requests: autoload (the default).
update_option( 'gdc_books_settings', $settings );

// Large, or only needed on one admin screen: do not autoload.
update_option( 'gdc_books_import_log', $big_log_array, false );
Watch out: A plugin that stores a few megabytes in an autoloaded option slows down every page of the site. Check Tools → Site Health → Info for the total autoloaded size; WordPress warns when it gets too large.

When not to use options

DataBetter home
Settings for the whole siteAn option
Data about one post (ISBN, price)Post meta — lesson 16
Data about one user (favourites)User meta: update_user_meta()
Temporary cached resultsA transient — lesson 21
Thousands of rows you need to queryA custom table — lesson 20

Storing every book's data in one giant option is a classic beginner mistake. It cannot be queried, it grows without limit, and two admins saving at once overwrite each other.

Practice

  • Write gdc_books_get_settings() with defaults and use it everywhere you read settings.
  • Save a large array with autoload off and confirm it in the wp_options table.
  • List three things your plugin stores and choose the right home for each.
Create a free account to save which lessons you've finished. Save my progress