Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use after_switch_theme action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
February 18, 2023
5 minutes read

after_switch_theme action

This action fires multiple times and the parameters differ according to the context, if the old theme exists or not. If the old theme is missing, the parameter will be the slug of the old theme.

To use after_switch_theme action, first you have to register it using add_action. You can write this code into functions.php of your activated theme or in a custom WordPress Plugin.

We at WePlugins always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.

In the below live example, we have defined a function execute_on_after_switch_theme_event which takes 2 parameters and we registered using add_action. The first parameter after_switch_theme is the name of the hook, The second parameter execute_on_after_switch_theme_event is the name of the function which needs to be called, the third parameter is the priority of calling the hook if the same hook is used multiple times and the last parameter is the number of arguments (if any) to be passed in the registered function.

Sometimes, you have to remove a registered hook so you can use remove_action to remove after_switch_theme action.

Parameters

    Below are the 2 parameters required to use this hook:

  • $old_name: (string) Old theme name.
  • $old_theme: (WP_Theme) WP_Theme instance of the old theme.

Live Example 1: Setting Up Theme Options

This example shows how you can set up default options for your theme when it’s activated:

add_action('after_switch_theme', 'weplugins_setup_options');
    function weplugins_setup_options() {
        add_option('weplugins_enable_catalog', 0);
        add_option('weplugins_enable_features', 0);
    }

Live Example 2: Custom Function Execution

Here’s how you can execute a custom function when the theme is switched:

function weplugins_execute_on_after_switch_theme_event($old_name, $old_theme){
        // Custom code to be executed when this action occurs in WordPress.
        // Use the parameters received in the function arguments and implement 
        // the required additional custom functionality according to your website requirements.
    }
    // Add the action
    add_action("after_switch_theme", "weplugins_execute_on_after_switch_theme_event", 10, 2);

Live Example 3: Removing a Hook Callback

To remove a hook callback, use the example below:

remove_action("after_switch_theme", "weplugins_execute_on_after_switch_theme_event", 10, 2);

Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.

Access Premium WordPress Plugins

Contact Us

If you need customization or are having any trouble using this hook, please Contact Us and we’d be happy to assist you.

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.