As WordPress developers, we frequently require a method to store as well as retrieve information. The most obvious cause for this is that we should save as well as recover plugins or theme configurations. However, there are a plethora of many other purposes why you might have to recover files.
As such, how then do we go about it? The possible alternatives API is the main tool we use at our disposal to accomplish this. That’s an API that allows us to retrieve information from the WordPress dashboard. Then we should know more about WordPress get_options
It’s almost as important as the plugin API. Now without it, we frequently can’t even build anything at all in WordPress. That is why it is worthwhile to dive a little deeper into how it appears to work.
Contents
The get_options functions
Functions related to get_alloptions: get_blog_option, get_site_option, get_user_option, delete_option
This is how it looks in WordPress 6.2
function get_option( $option, $default = 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 );
}
/**
* 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` parameter was added.
*
* @param mixed $pre_option The value to return instead of the option value. This differs
* from `$default`, 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 The fallback value to return if the option does not exist.
* Default false.
*/
$pre = apply_filters( "pre_option_{$option}", false, $option, $default );
/**
* 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`, 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 The fallback value to return if the option does not exist.
* Default false.
*/
$pre = apply_filters( 'pre_option', $pre, $option, $default );
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() ) {
// Prevent non-existent options from triggering multiple queries.
$notoptions = wp_cache_get( 'notoptions', 'options' );
// Prevent non-existent `notoptions` key from triggering multiple key lookups.
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 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, $option, $passed_default );
}
$alloptions = wp_load_alloptions();
if ( isset( $alloptions[ $option ] ) ) {
$value = $alloptions[ $option ];
} else {
$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.
if ( ! is_array( $notoptions ) ) {
$notoptions = array();
}
$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, $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, $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 );
}
The database functions
- The possibilities API, at its core, is an API that allows us to communicate with the wp_options data store. We could use it to insert, eliminate, as well as keep updating value systems in the data structure. There are four sections in the database table:
- option_id
- option_name
- option_value
- as well as self-selection.
- The unique identifier of the wp_options table is option_id. Every time MySQL adds a new row to the table, it autoincrements. In practice, however, WordPress makes no use of it.
- The option_name, as well as option_value sections, are the most essential columns in the wp_options table. Such two are used by WordPress as a shared key. This is a type of data portrayal in which data is assigned to a key. Implicit storage systems in PHP work the same way.
- The possible alternatives API doesn’t anticipate you to recover your data using option_id. This doesn’t even allow you to use it. It prefers that you will always be using a key to back up your data. As well as you use the same key to get it back.
- The autoload section would be the last in the wp_options table. It is a flag that tells WordPress to choose whether or not to autoload. The symbol should have one of two possible values: yes or even no.
Autoloading is a choice.
- So, what makes autoloading a choice unique? So, assume you have to understand the worth of a particular option. To get that choice from the database, you’ll just use possibilities API. And then to do so, WordPress will have to run a SQL query.
- Let’s assume you require some other value. WordPress would have to search the database once more to obtain it. Making SQL statements in this manner does not measure that well.
- To address this issue, WordPress invented the notion of achieving the intended options.
- WordPress would then load in memory only those possibilities with the autoload flag established to yes.
- This decreases the number of SQL requests that WordPress must perform to retrieve choices from the database.
- In terms of code, the wp load all options feature is in charge of loading all autoloaded. In practice, you should not be using this purpose very often.
- When you’re using the API, this would contact the wp to load all inquiries, so each time necessary to load all of them achieving the intended choices.
API operations
Okay, that pretty much describes it all related to the wp_options table in the database. Let’s now take a look at the API functions themselves. We’ll divide them into 4 categories: trying to add, remove, obtain, as well as upgrade possibilities.
API feature types
- To begin, it’s important to note that each choice API feature is accessible in 2 flavors: frequent and multipoint. The frequent features are the ones that the majority of us use. They interact with the wp_options table, which we just discussed.
- In the meantime, the multisite variants all have _site_ throughout their names. All multisite options API features act similarly to their frequent counterpart. They do, however, have two architectural differences worth mentioning.
- The main distinction is that multisite features do not use the wp options table. They make use of a separate table named wp site meta. It’s a desk that businesses options for entire article connections.
- Another distinction is that web options (as we refer to them) all are autoloaded whether you want them to or not. You don’t have a sign you could indeed set to regulate whether or not WordPress would then autoload them. This may appear to be a significant change, but it is not.
- The reality is that we rarely overwrite the default autoload flag. We end up leaving it alone. This implies that WordPress could well automatically load the majority of the possibilities we add.
What if I’m not sure which one to use?
- It can be inconvenient to have to consider which form of features to be using. That’s why, in addition to the previous we’ve seen, there’s a 3rd, lesser-known collection of functions you could use. This is a list of network features.
- The functionalities, like the multisite operations, are named _network_. However, unlike the other 2 kinds of API features, network API functions do not communicate with a given database list.
- They used the wp_options or wp site meta table depending on whether WordPress does use aspects of the website or not.
- When you’re not sure what type of role to use, such features could indeed come in handy. They’ll make sure that your plugin has only each array of choices, regardless of whether someone does use aspects of the website.
- To maintain those features in mind for those scenarios.
- Having said that, the rest of this section would then concentrate on the frequent and multipoint kinds of components. This is because these are the features you will use most. They are also in charge of interacting with the WordPress database tables.
Including a choice
- The first set of activities we’ll look at is for introducing new possibilities. The add option function adds a whole new choice to the wp_options table. It takes four inputs: valuation, deprecated, as well as autoload.
- The majority of these variables correspond to what we’ve discussed so far about the API. The title of the choice that we all want to add is choice. In the meantime, a valuation will be saved in the wp table.
- This third variable has been dismissed (hence the name!) As a result, you must always transfer it to an empty value.
- The final option is autoloaded. This is the autoload sign we’ve been discussing, and the possible value is yes.
- The added site feature is another one we have. It adds a whole new possibility to the wp site metadatabase table.
- It works with two parameters: option as well as value. These are the same two main parameters that the add option feature uses.
- Both activities return the value true or false. The function returns true if it was successful in adding the option. Normally, the function will return false.
Option removal
- To remove an option from the wp table, use “delete”.
- To delete site function takes a site choice from the wp site meta table.
Getting an option
After you’ve deleted an option, let’s take a look at the features you could use to get a choice. We have 2 purposes as regular:
- get_option
- site_option.
The first is for the wp options list, while the second is for the wp site meta table.
Option and default are the two main parameters used by both features. an option is the name of the choice we’re attempting to obtain. The features will return the value if they discover one using the variables. Normally, they’ll return a value you managed to pass as the activities’ default argument.
Changing a setting
- Finally, we have the features that we use to keep updating an existing Website. For the wp_options board, we have the update. As well as the wp site meta table’s update site feature.
- Both features made use of equivalent parameters. Both of them make use as well as valuation parameters. the value is the name to be updated. And in the meantime, value denotes the added features that will be assigned to the established choice.
- Nevertheless, there is a third parameter to the update feature: autoload. This parameter allows you to change that means the image sign and its value. Because the wp site meta isn’t using the autoload sign, the update site_option does not use that variable.
In conclusion
we knew all things related to WordPress get_options through the previous points inside this blog post and we hope you like it too much my developer mate.