admin管理员组文章数量:1130349
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.
1 Answer
Reset to default 2You'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', [] );
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.
1 Answer
Reset to default 2You'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', [] );
本文标签: optionsIs there an optimizedWordPressy way to not call a getoption twice
版权声明:本文标题:options - Is there an optimized, WordPress-y way to not call a `get_option` twice? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749189915a2330015.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论