Using WordPress ‘populate_options()’ PHP function

The populate_options() WordPress PHP function creates WordPress options and sets their default values.

Usage

populate_options( $options = array() )

Example:

$options = array(
    'my_option_1' => 'value_1',
    'my_option_2' => 'value_2',
);

populate_options( $options );

Parameters

  • $options (array) (Optional): Custom option $key => $value pairs to use. Default is an empty array.

More information

See WordPress Developer Resources: populate_options

Examples

Create a simple option with default value

This example creates a new option named ‘my_theme_color’ with a default value of ‘blue’.

$options = array(
    'my_theme_color' => 'blue',
);

populate_options( $options );

Create multiple options with default values

This example creates multiple options with their respective default values.

$options = array(
    'my_theme_color'   => 'blue',
    'my_theme_font'    => 'Arial',
    'my_theme_fontsize' => '16px',
);

populate_options( $options );

Create a nested array of options with default values

This example creates a nested array of options with default values.

$options = array(
    'my_theme_settings' => array(
        'color'   => 'blue',
        'font'    => 'Arial',
        'fontsize' => '16px',
    ),
);

populate_options( $options );

Update an existing option with a new default value

This example updates an existing option named ‘my_theme_color’ with a new default value of ‘green’.

$options = array(
    'my_theme_color' => 'green',
);

populate_options( $options );

Create an option with a numeric default value

This example creates a new option named ‘my_theme_columns’ with a default value of 3.

$options = array(
    'my_theme_columns' => 3,
);

populate_options( $options );