2

I have a custom options page. I have several sections of settings and some of them are even more closely related to each other so I decided to store them in arrays, seems more logical to me. It surely, minimizes the code a bit too. First question: Is this a good practice?

Example:

get_option('header')['header_logo']['image'];
get_option('header')['header_logo']['width'];
get_option('header')['header_logo']['height'];
get_option('header')['header_color'];

This results to an array which includes nested arrays at different levels. If we use get_option('header'); normally and supposedly store a string, we can use get_option('header', 'default value'); to echo a default value if the option is not set. How can I do this for a value within an array ex. get_option('header')['header_logo']; and similarly for get_option('header')['header_logo']['image'];? Also how can this be done for all items in the whole array at once?

Second question: Which is the best practice when it comes to setting default option values? Should I use the update_option function in the after_theme_setup function to set the defaults? Is there any better way?

Thanks in advance.

1 Answer 1

4

First question: Is this a good practice?

Based on the type of value you are saving, array as settings value is definitely good practice.


How can I do this for a value within an array ex. get_option('header')['header_logo']['image']?

If you believe option value for header might be empty or not saved, you can use this -

get_option( 
  'header', 
  array( 
    'header_logo' => array(
      'image' => '.../default-image.png',
      'width' => '200',
      'height' => '60'
    )
  )
)['header_logo']['image']

However, if the option header contains any value (even empty array), then the above option will not override the default. In that case, you could write a wrapper function for get_option function.

function wpse_get_header_settings( $group = '', $key = '', $default = null ) {

  $settings = get_option( 'header' );

  $defaults = array( 
    'header_logo' => array(
      'image' => '.../default-image.png',
      'width' => '200',
      'height' => '60'
    )
  );

  $settings = wp_parse_args( $settings, $defaults );

  if ( ! empty( $group ) ) {
    if ( ! array_key_exists( $group, $settings ) ) {
      return $default;
    }

    $settings = $settings[ $group ];

    if ( ! empty( $key ) ) {
      return array_key_exists( $key, $settings ) ? $settings[ $key ] : $default;
    }
  }

  return $settings;
}

And then, you can safely get any desired value -

// would return the logo image
echo wpse_get_header_settings( 'header_logo', 'image' );

// would return the header text color
wpse_get_header_settings( 'header_color' );

Which is the best practice when it comes to setting default option values?

It's better to save default option values using after_theme_setup for theme and register_activation_hook for plugin. But, if you use a wrapper function to call settings, you shouldn't save it.

4
  • Shazzad, thank you very much this is a great answer. I am glad you've showed me the wrapper function in any case, however isn't this function unnecessary if I set defaults after theme setup?
    – Arg Geo
    Apr 11, 2020 at 20:36
  • Yes, it's unnecessary if you set defaults after theme setup.
    – Shazzad
    Apr 11, 2020 at 20:39
  • Am I right to say that you can get rid of possible error/warnings such as "illegal string" when using arrays by updating options with default values?
    – Arg Geo
    Apr 11, 2020 at 20:42
  • Definitely. if called property exists within array, no warning will be given.
    – Shazzad
    Apr 11, 2020 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.