A Comprehensive Guide to Setting Options on Theme Activation in WordPress

Home » Snippets » A Comprehensive Guide to Setting Options on Theme Activation in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Activating a new WordPress theme can be an exciting prospect, but it’s not without its challenges. Setting specific options such as layout, widgets, menus, and more is a crucial part of tailoring your theme to meet your needs. However, many WordPress users are unsure how to effectively set these options upon activation. In this article, we’ll guide you through the process of setting up your theme options as soon as you activate a new theme on your WordPress site.

					function wpturbo_set_theme_options() {
    if (!get_option('wpturbo_theme_options')) { 
        $default_options = array(
            'option_1' => 'default value 1',
            'option_2' => 'default value 2',
            //add your other options default values here
        );
        add_option('wpturbo_theme_options', $default_options);
    }
}
add_action('after_switch_theme', 'wpturbo_set_theme_options');
				

This snippet is responsible for setting up theme options when a new theme is activated in WordPress.

We start by defining a new function, wpturbo_set_theme_options(). This function checks if the option wpturbo_theme_options exists, and if it does not exist, it creates it.

function wpturbo_set_theme_options() {
    if (!get_option('wpturbo_theme_options')) {
}

The get_option function is used to retrieve the current value of all setting fields named wpturbo_theme_options. If it can’t find any values for this particular option, it gives a return of FALSE, which triggers the condition for the function to run.

Inside the if statement, we define a new array $default_options and initialize it with some default values. In this example, the array $default_options is set to ‘default value 1’ and ‘default value 2’ respectively.

$default_options = array(
    'option_1' => 'default value 1',
    'option_2' => 'default value 2',
    //add your other options default values here
);

This list can be extended as per requirement by adding more (‘option’ => ‘default value’) pairs.

Next, we utilize the WordPress function add_option to store information in the WordPress database specifically within the {wp database prefix}_options table.

add_option('wpturbo_theme_options', $default_options);

The add_option function is an inbuilt function in WordPress that is used to add a new option to the options database table. The first parameter of add_option is the name of the option to add, and the second one is the value of the option.

Finally, the add_action function hooks wpturbo_set_theme_options into the after_switch_theme action. This is an in-built WordPress hook that is fired when the blog’s theme is changed; it triggers only once after the theme has been switched.

add_action('after_switch_theme', 'wpturbo_set_theme_options');

The ‘after_switch_theme’ hook runs after the theme is switched but before the new theme’s functions.php file is loaded. In this way, our function is triggered as soon as the theme is switched (activated), thus, automatically setting up our theme options.

Register an account to save your snippets or go Pro to get more features.