[wp-trac] [WordPress Trac] #51699: The only correct method to check for existence of option

WordPress Trac noreply at wordpress.org
Mon Feb 8 13:49:12 UTC 2021


#51699: The only correct method to check for existence of option
--------------------------------+------------------------------
 Reporter:  ttodua              |       Owner:  (none)
     Type:  defect (bug)        |      Status:  reopened
 Priority:  normal              |   Milestone:  Awaiting Review
Component:  Options, Meta APIs  |     Version:
 Severity:  normal              |  Resolution:
 Keywords:                      |     Focuses:
--------------------------------+------------------------------

Comment (by apedog):

 Replying to [comment:2 SergeyBiryukov]:
 >
 > 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.';
 > } else {
 >       echo 'The option exists.';
 > }
 > }}}
 Sergey's solution above (I've edited it slightly), in fact, does not work.

 If a plugin makes use of the {{{"pre_option_{$option}"}}} filter (which
 shortcircuits {{{get_option}}} to return a custom value), this solution
 will show that the option exists - even when no such option exists in the
 options table.

 An {{{option_exists}}} API function would actually solve for such a
 situtation.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/51699#comment:7>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list