Skip to content
Advertisement

WordPress – Add a default value to get_option if it’s used as a variable

How to add a default value to get_option if it’s used as a variable ?

The code :

JavaScript

I want to avoid an Undefined index error or a false return.

Is the following correct ?

JavaScript

Advertisement

Answer

As you’ve written it, you’re only catching Undefined Index errors. There’s also a parenthesis out of place that renders it syntactically meaningless. My opinion here is a ternary operator gets ugly with that much text. This would do exactly the same thing. You can always make things more elegant once you understand them.

JavaScript

Edit:

You could also use empty to achieve this in less words.

JavaScript

From there your ternary looks a little better:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement