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:
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.
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.
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 | 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 |
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.
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.
How to Create a Custom Table in WordPress via functions.php: A Complete Guide to MySQL Data Types
212
HTML Select: hidden placeholder, highlight, and dynamic urgency indicator
221
How to Create a Custom Admin Menu in WordPress: A Simple Guide for Beginners
169
Customizing the WordPress Login Page: Logo, Colors, Background Image & Custom CSS
204
How to Change or Completely Remove the Text in the WordPress Admin Footer
214
How to Display the Visitor’s IP Address on a Website Using PHP? 575
Adding Meta Description and Keywords in WordPress 410
How to Detect an AdBlocker on Your Site Using JavaScript 322
How to Add a Currency Widget to the WordPress Admin Dashboard 252
HTML Select: hidden placeholder, highlight, and dynamic urgency indicator 221
Leave a Reply