How to add new options to my WordPress plugin

Question

When you fetch the options, don’t assume that get_option succeeded, check the returned value before using it, and if the value is exactly equal to false, set it to a default value.

For example:

$mysetting = get_option( 'mysetting' );
if ( $mysetting === false ) {
    $mysetting = "default value";
}
// ... use mysetting here

You can also use the second parameter of get_option, e.g.:

$mysetting = get_option( 'mysetting', 'default value' );
// ... use mysetting here

Also consider registering your setting using register_setting which provides a default parameter.

https://developer.wordpress.org/reference/functions/register_setting/

0
Marco Gasi 5 months 2022-03-22T14:57:26-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse