Activation and Initialization Hooks in WordPress: Where and When to Create Tables


Choosing the right hook to create a table is not just a matter of “will the code work,” but a matter of your site’s performance. If you choose the wrong moment to call dbDelta, you could slow down your site or create unnecessary load on the database.

Let’s look at the main options and their features:

1. register_activation_hook

This is the “gold standard” for plugin developers.

// Call the function when the plugin is activated
register_activation_hook(__FILE__, 'create_custom_table');

Full Example:

function create_custom_table() {
    echo "Hello, world! I am a plugin, and I have just activated.";
}
// Call the function when the plugin is activated
register_activation_hook(__FILE__, 'create_custom_table');
  • How it works: The code runs only once — the moment the user clicks the “Activate” button in the plugin list.

  • Pros: Maximum performance. WordPress does not check the table every time a visitor views the site.

  • Important: The __FILE__ parameter must be located in the main plugin file.

2. after_switch_theme





The activation equivalent specifically for themes. Used in functions.php.

// Call the function when the theme is activated
add_action('after_switch_theme', 'create_custom_table');

Full Example:

function create_custom_table() {
    echo "Hello, world! I am a theme, and I have just activated.";
}
// Call the function when the theme is activated
add_action('after_switch_theme', 'create_custom_table');
  • How it works: Triggers once, immediately after you switch the theme in the admin dashboard to the current one.

  • Pros: Perfect for themes. The database is not burdened with extra checks during normal site operation.

3. after_setup_theme

The theme primary setup hook, which runs constantly.

// Run the function on every theme load
add_action('after_setup_theme', 'create_custom_table');

Full Example:

function create_custom_table() {
    echo "Hello, world! I am checking the table on every page load.";
}
// Run the function on every theme load
add_action('after_setup_theme', 'create_custom_table');
  • How it works: This hook triggers on every single page load as soon as the theme is initialized.

  • Cons: Since the table check occurs every time a user visits the site, it creates an unnecessary load on the server.

Hook Comparison: Which one to choose?

Hook Project Type Run Frequency Verdict
register_activation_hook Plugin Once Best for plugins
after_switch_theme Theme Once Best for themes
after_setup_theme Theme Every request For development only

Pro Tip: Data Cleanup

When you create a table, it is good practice to plan for its removal. For plugins, the uninstall.php file is used for this purpose.

Important: Never delete a table containing user data during a standard plugin deactivation. Only delete it during a full uninstall, so users don’t lose their information accidentally.

Summary

  • If you are writing a plugin — use register_activation_hook.

  • If you are writing a theme — use after_switch_theme.

  • Use after_setup_theme only if you truly need a constant check.