[wp-trac] [WordPress Trac] #41099: update_option return value ambiguous
WordPress Trac
noreply at wordpress.org
Tue Jun 20 21:20:25 UTC 2017
#41099: update_option return value ambiguous
--------------------------------+------------------------------
Reporter: cloughit | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Options, Meta APIs | Version: 4.8
Severity: normal | Resolution:
Keywords: | Focuses:
--------------------------------+------------------------------
Comment (by cloughit):
@mdifelice @alexvorn2 My issue differs as I am concerned with the return
values of 'update_option' rather than trying to save a boolean (in your
case 'false').
Consider a scenario where option 'foo' exists with value 'bar':
update_option( 'foo', 'ram' ) - returns true (Line 386)
update_option( 'foo', 'bar' ) - returns false (Line 308 - option exists
and is the same value)
update_option( 1, 'ram' ) - returns false (Line 344 - error inserting in
database)
update_option( ' ', 'ram' ) - returns false (Line 263 - empty option name)
If (as it is in my case) it is necessary to provide user feedback on the
status of an option update (such as 'Settings Saved!), there is no way to
differentiate if the returned 'false' value is due to option exists / same
or an error occurred saving.
By returning NULL at Line 308 it would be possible to then differentiate
and handle responses correctly without the overhead of calling
'get_option'. For example:
{{{#!php
$update = update_option( 'foo', 'bar' );
if ( false === $update ) {
{... show 'Error Saving Settings' message ...}
} else if ( is_null( $update ) ) {
{... show 'No Changes Made' message ...}
} else {
{... show 'Settings Saved!' message ...}
}
}}}
Currently the only way to achieve the desired result:
{{{#!php
$update = ( get_option( 'foo' ) == 'bar' ) ? NULL : update_option( 'foo',
'bar' ); // returns either (bool)true, (bool)false or NULL
if ( false === $update ) {
{... show 'Error Saving Settings' message ...}
} else if ( is_null( $update ) ) {
{... show 'No Changes Made' message ...}
} else {
{... show 'Settings Saved!' message ...}
}
}}}
I believe that this differentiation should occur within the
'update_option' function.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/41099#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list