$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>options - Is there an optimized, WordPress-y way to not call a `get_option` twice?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

options - Is there an optimized, WordPress-y way to not call a `get_option` twice?

matteradmin10PV0评论

I'm currently trying to get an option and if it's not present, simply assign an empty array as this variable:

$option = get_option( 'option_name' ) ? get_option( 'option_name' ) : [];

The problem is that not only is this ugly when the option name gets a little bit complicated, but I'm making the same call twice.

What are my options here?

One way would be:

$option = get_option( 'option_name' );

//If there is something in that $option and it's an array
if( $option !== null && is_array( $option ) ) {
    //Proceed with logic.
}

But this also seems rather complicated. Please keep in mind that I write for PHP 5.4, so ?? is out of the question.

I'm currently trying to get an option and if it's not present, simply assign an empty array as this variable:

$option = get_option( 'option_name' ) ? get_option( 'option_name' ) : [];

The problem is that not only is this ugly when the option name gets a little bit complicated, but I'm making the same call twice.

What are my options here?

One way would be:

$option = get_option( 'option_name' );

//If there is something in that $option and it's an array
if( $option !== null && is_array( $option ) ) {
    //Proceed with logic.
}

But this also seems rather complicated. Please keep in mind that I write for PHP 5.4, so ?? is out of the question.

Share Improve this question asked Nov 12, 2018 at 14:14 coolpastacoolpasta 9691 gold badge9 silver badges24 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

You're not actually querying the database twice, if that's what you're worried about.

Regardless, as you'll see from the documentation, get_option() has a second argument you can use to define a default value for if the option hasn't been set:

$option = get_option( 'option_name', [] );
Post a comment

comment list (0)

  1. No comments so far