Check if an option exists and get its value in one hit
I have an option admins_settings
that has an array of sub-options that is stored in the wp_options
table. Before I get its value from the table, I check if this field exists by running this code:
if ( !isset( get_option( 'admins_settings' )['option_name'] ) ) {
return false;
} else {
return get_option( 'admins_settings' )['option_name'];
}
Here, I'm hitting the table 2 times (if the field exists) to get its value. Is there any way to get the value of the field with one hit?
I know that the get_option
function accepts a default value if the option doesn't exist. But this works with the simple values:
get_option( 'admins_settings', false );
But not with array values:
get_option( 'admins_settings' )['option_name'];
Because the option admins_settings
could be there, but it doesn't have that item option_name
.
Again, is there any way to get the sub-value of the field with one hit?