How to add new options to my WordPress plugin


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.

Marco Gasi 2 years 2022-03-22T14:57:26-05:00 0 Answers 0 views 0

Leave an answer