What is the purpose of the uninstall hook in WordPress plugins?
The uninstall hook is used to clean up all plugin data like options, custom tables, and settings when the plugin is deleted, ensuring no leftover data remains.
Click to reveal answer
beginner
Which file should contain the uninstall hook code in a WordPress plugin?
The uninstall hook code should be placed in an uninstall.php file or handled via register_uninstall_hook() in the main plugin file.
Click to reveal answer
intermediate
How do you register an uninstall hook in a WordPress plugin?
Use register_uninstall_hook(__FILE__, 'function_name') in the main plugin file to specify the cleanup function to run on uninstall.
Click to reveal answer
intermediate
Why should you avoid using deactivation hooks for cleanup tasks?
Deactivation hooks run when a plugin is turned off but not deleted, so cleanup here could remove data users want to keep. Cleanup belongs in uninstall hooks.
Click to reveal answer
beginner
What kind of data should you remove in the uninstall hook?
Remove all plugin-created options, custom database tables, user meta, and files that are no longer needed to keep the site clean.
Click to reveal answer
Where should you place the uninstall cleanup code in a WordPress plugin?
AIn uninstall.php or via register_uninstall_hook()
BIn the main plugin activation function
CInside the plugin's stylesheet
DIn the theme's functions.php file
✗ Incorrect
Uninstall cleanup code belongs in uninstall.php or registered with register_uninstall_hook() to run only when the plugin is deleted.
What happens if you only use the deactivation hook for cleanup?
ANothing happens on deactivation
BData is removed only when plugin is deleted
CData is removed when plugin is turned off, which may be unwanted
DPlugin automatically reactivates
✗ Incorrect
Deactivation hook runs when plugin is turned off, so removing data here can cause loss if user just wants to pause the plugin.
Which function registers an uninstall hook in WordPress?
Aadd_filter('cleanup')
Bregister_uninstall_hook()
Cregister_deactivation_hook()
Dadd_action('uninstall')
✗ Incorrect
register_uninstall_hook() is the correct function to register a cleanup function for plugin uninstall.
What kind of plugin data should NOT be left behind after uninstall?
ATheme files
BPlugin images used by other plugins
CWordPress core files
DOptions, custom tables, user meta
✗ Incorrect
Plugin-specific data like options and custom tables should be removed to avoid cluttering the database.
If a plugin does not have an uninstall hook, what happens when it is deleted?
APlugin files are deleted but data remains in the database
BPlugin files and data are fully removed
CPlugin cannot be deleted
DWordPress crashes
✗ Incorrect
Without an uninstall hook, only plugin files are deleted; data like options remain in the database.
Explain how to properly clean up plugin data when uninstalling a WordPress plugin.
Think about what happens when a plugin is deleted and how to remove all traces.
You got /4 concepts.
Describe the difference between deactivation and uninstall hooks in WordPress plugins.
Consider when each hook runs and what should happen to plugin data.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using register_uninstall_hook in a WordPress plugin?
easy
A. To register a new shortcode for the plugin
B. To add a settings page in the admin dashboard
C. To clean up plugin data when the plugin is deleted
D. To enqueue scripts and styles for the plugin
Solution
Step 1: Understand the role of uninstall hooks
Uninstall hooks are designed to remove all plugin data when the plugin is deleted to keep the site clean.
Step 2: Identify the function of register_uninstall_hook
This function links a cleanup function that runs on plugin deletion to remove options, transients, and custom tables.
Final Answer:
To clean up plugin data when the plugin is deleted -> Option C
Quick Check:
Uninstall hook purpose = clean up data [OK]
Hint: Uninstall hooks remove plugin data on deletion [OK]
Common Mistakes:
Confusing uninstall hooks with activation hooks
Thinking uninstall hooks add features instead of cleaning
Using uninstall hook to enqueue scripts
2. Which of the following is the correct way to register an uninstall hook in a WordPress plugin?
easy
A. register_activation_hook(__FILE__, 'my_cleanup_function');
B. add_action('uninstall_plugin', 'my_cleanup_function');
C. add_filter('uninstall_hook', 'my_cleanup_function');
D. register_uninstall_hook(__FILE__, 'my_cleanup_function');
Solution
Step 1: Recall the correct function for uninstall hooks
The correct function to register an uninstall hook is register_uninstall_hook with the plugin file and cleanup function.
Step 2: Check the syntax of each option
register_uninstall_hook(__FILE__, 'my_cleanup_function'); uses the correct function and parameters. Options A, B, and D use incorrect functions or hooks.
Final Answer:
register_uninstall_hook(__FILE__, 'my_cleanup_function'); -> Option D
Hint: Use register_uninstall_hook with __FILE__ and function name [OK]
Common Mistakes:
Using activation hook instead of uninstall hook
Using add_action with wrong hook name
Confusing filters with hooks
3. Consider this uninstall function in a plugin:
function cleanup_plugin() {
delete_option('plugin_setting');
delete_transient('plugin_cache');
global $wpdb;
$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}plugin_data");
}
What will happen when the plugin is uninstalled?
medium
A. All plugin data including options, transients, and custom tables will be removed
B. Only the option 'plugin_setting' will be deleted
C. The transient 'plugin_cache' will be deleted but the table remains
D. Nothing will be deleted because uninstall hooks do not run functions
Solution
Step 1: Analyze the cleanup function actions
The function deletes an option, a transient, and drops a custom database table using SQL.
Step 2: Understand uninstall hook behavior
When the uninstall hook calls this function, all these cleanup actions will run, removing all plugin data.
Final Answer:
All plugin data including options, transients, and custom tables will be removed -> Option A
Quick Check:
Cleanup function deletes all data = All plugin data including options, transients, and custom tables will be removed [OK]
Hint: Cleanup function removes all data types explicitly [OK]
Common Mistakes:
Assuming uninstall hooks do not run cleanup functions
Thinking only options or transients are deleted
Ignoring the SQL DROP TABLE command
4. Identify the error in this uninstall hook registration:
register_uninstall_hook('my-plugin/my-plugin.php', 'cleanup_plugin');
function cleanup_plugin() {
delete_option('my_plugin_option');
}
medium
A. The plugin file path should use __FILE__ instead of a string
B. The cleanup function name must be prefixed with 'uninstall_'
C. delete_option cannot be used in uninstall functions
D. The uninstall hook must be registered inside the cleanup function
Solution
Step 1: Check the first parameter of register_uninstall_hook
The first parameter should be the plugin main file path, usually passed as __FILE__ for reliability.
Step 2: Understand why using a string path is problematic
Using a string path can cause the hook not to trigger if the path is incorrect or changes; __FILE__ ensures correct reference.
Final Answer:
The plugin file path should use __FILE__ instead of a string -> Option A
Quick Check:
Use __FILE__ for plugin path in uninstall hook [OK]
Hint: Always use __FILE__ for plugin path in uninstall hook [OK]
Common Mistakes:
Using string path instead of __FILE__
Thinking function name must have uninstall prefix
Registering hook inside cleanup function
5. You want to ensure your plugin cleans up all its data on uninstall, including options, transients, and a custom table named wp_custom_data. Which of the following uninstall functions correctly performs this cleanup?
hard
A. function uninstall_plugin() {
delete_option('plugin_option');
delete_transient('plugin_transient');
global $wpdb;
$wpdb->query("DROP TABLE wp_custom_data");
}
B. function uninstall_plugin() {
delete_option('plugin_option');
delete_transient('plugin_transient');
global $wpdb;
$wpdb->query("DROP TABLE IF EXISTS wp_custom_data");
}
C. function uninstall_plugin() {
delete_option('plugin_option');
delete_transient('plugin_transient');
global $wpdb;
$wpdb->query("DELETE FROM wp_custom_data");
}
D. function uninstall_plugin() {
delete_option('plugin_option');
delete_transient('plugin_transient');
}
Solution
Step 1: Review each cleanup action
function uninstall_plugin() {
delete_option('plugin_option');
delete_transient('plugin_transient');
global $wpdb;
$wpdb->query("DROP TABLE IF EXISTS wp_custom_data");
} deletes the option, transient, and drops the custom table safely using IF EXISTS.
Step 2: Compare with other options
function uninstall_plugin() {
delete_option('plugin_option');
delete_transient('plugin_transient');
global $wpdb;
$wpdb->query("DROP TABLE wp_custom_data");
} drops table without IF EXISTS (may cause error if table missing). function uninstall_plugin() {
delete_option('plugin_option');
delete_transient('plugin_transient');
global $wpdb;
$wpdb->query("DELETE FROM wp_custom_data");
} deletes rows but not table. function uninstall_plugin() {
delete_option('plugin_option');
delete_transient('plugin_transient');
} misses table cleanup.
Final Answer:
function uninstall_plugin() {
delete_option('plugin_option');
delete_transient('plugin_transient');
global $wpdb;
$wpdb->query("DROP TABLE IF EXISTS wp_custom_data");
} -> Option B
Quick Check:
Safe drop table + delete option and transient = function uninstall_plugin() {
delete_option('plugin_option');
delete_transient('plugin_transient');
global $wpdb;
$wpdb->query("DROP TABLE IF EXISTS wp_custom_data");
} [OK]
Hint: Use DROP TABLE IF EXISTS to safely remove custom tables [OK]
Common Mistakes:
Dropping table without IF EXISTS causing errors
Deleting rows instead of dropping table
Forgetting to remove custom tables
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.