[wp-trac] [WordPress Trac] #51699: The only correct method to check for existence of option
WordPress Trac
noreply at wordpress.org
Tue Nov 3 18:34:17 UTC 2020
#51699: The only correct method to check for existence of option
--------------------------------+------------------------------
Reporter: ttodua | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Options, Meta APIs | Version:
Severity: normal | Resolution:
Keywords: | Focuses:
--------------------------------+------------------------------
Changes (by SergeyBiryukov):
* component: General => Options, Meta APIs
Comment:
Thanks for the ticket!
Just noting that
[https://developer.wordpress.org/reference/functions/get_option/
get_option()] has an optional `$default` argument, which serves exactly
this purpose, it's a default value to return if the option does not exist.
So the existence could be checked with a snippet like this:
{{{
if ( 'not-exists' === get_option( 'my_option', 'not-exists' ) ) {
echo 'The option does not exist.';
}
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/51699#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list