WordPress

WordPress TutorialRemove Version from WordPress and StylesheetsRun WordPress local with XAMPPSecurity in WordPress - EscapingSecurity in WordPress - SanitizationThe Loop (main WordPress loop)Update WordPress ManuallyWordPress Actions and FiltersWordPress add action()WordPress add editor style()WordPress add menu page()WordPress Add ShortcodeWordPress add submenu page()WordPress add theme support()WordPress Add/remove contact info for users with user contactmethods filter hookWordPress Admin Dashboard WidgetsWordPress AJAXWordPress Alternating main loop (pre get posts filter)WordPress Child Theme BasicsWordPress Create a Post ProgrammaticallyWordPress Create Template for Custom Post TypeWordPress Creating a custom templateWordPress Custom exerpts with excerpt length and excerpt moreWordPress Custom Post TypesWordPress Customizer Basics (Add Panel, Section, Setting, Control)WordPress Customizer Hello WorldWordPress DebuggingWordPress Enqueuing scriptsWordPress Enqueuing StylesWordPress Function : wp trim words()WordPress Function: add action()WordPress get bloginfo()WordPress get home path()WordPress get option()WordPress get permalink()WordPress get template part()WordPress get the category()WordPress get the title()WordPress home url()WordPress How Can I integrate Markdown editor with Advance Custom Fields repeater Add-on.WordPress initWordPress Installation and ConfigurationWordPress Making network requests with HTTP APIWordPress Meta BoxWordPress Options APIWordPress Plugin creationWordPress Plugin developmentWordPress Post FormatsWordPress Querying postsWordPress Remove Auto Line Breaks from Content and ExcerptWordPress REST APIWordPress Secure your installationWordPress ShortcodeWordPress Shortcode with attributeWordPress ShortcodesWordPress SidebarsWordPress Site MigrationWordPress TaxonomiesWordPress Template hierarchyWordPress template includeWordPress the $wpdb ObjectWordPress the Admin Bar (aka The Toolbar)WordPress the title()WordPress theme and child-theme developmentWordPress ThemesWordPress wp get current user()WordPress WP Query() LoopWordPress WP-CLIWordPress WP-Cron



WordPress Options API

From WikiOD

Options are pieces of data that WordPress uses to store various preferences and configuration settings. The Options API is a simple and standardized way of storing data in the database. The API makes it easy to create, access, update, and delete options.

Syntax[edit | edit source]

  • // Create new option within WordPress

add_option( $option, $value = , $deprecated = , $autoload = 'yes' );

  • // Removes an option from the database.

delete_option( $option );

  • // Retrieve a saved option

get_option( $option, $default = false );

  • // Update the value of an option that was already added.

update_option( $option, $newvalue );

  • // There are also *_site_option() versions of these functions,

// to manipulate network-wide options in WordPress Multisite

  • // Create new network option

add_site_option( $option, $value = , $deprecated = , $autoload = 'yes' );

  • // Removes a network option

delete_site_option( $option );

  • // Retrieve a saved network option

get_site_option( $option, $default = false );

  • // Update the value of an option that was already added.

update_site_option( $option, $newvalue );

Remarks[edit | edit source]

The Options API is a simple and standardized way of working with data staored in the options table of MySQL database. The API makes it easy to create, read, update, and delete options.

get_option[edit | edit source]

get_option function is used to retrieve a value from from options table based on option name.

You can use the following code to get email address of a WordPress site administrator.

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

get_option() has an optional 2nd argument, which allows you to set a default value to return in the case that the requested option isn't set. By default, this argument is false.

To retrieve a text string, and use a boilerplate string if the text isn't set in the options table, you could do this:

<?php get_option( 'my_text', "I don't have anything written. Yet." ); ?>

add_option[edit | edit source]

add_option function ins used to insert new row into options table.

This will insert a new row in options table with option name some_option_name and value as some_option_value

<?php add_option( 'some_option_name', 'some_option_value' ); ?>

delete_option[edit | edit source]

delete_option function is used to delete an option from the options table.

This will delete my_custom_option from the options table.

<?php delete_option( 'my_custom_option' ); ?>

update_option[edit | edit source]

update_option function is used to update a value that already exists in the options table. If the option does not exist, then the option will be added with the option value.

This will set the default comment status to 'closed':

update_option( 'default_comment_status', 'closed' );

This article is an extract of the original Stack Overflow Documentation created by contributors and released under CC BY-SA 3.0. This website is not affiliated with Stack Overflow