Вопрос

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.

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с wordpress.stackexchange
scroll top