Bird
Raised Fist0
Wordpressframework~10 mins

Uninstall and cleanup hooks in Wordpress - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to register an uninstall hook for a WordPress plugin.

Wordpress
register_uninstall_hook(__FILE__, '[1]');
Drag options to blanks, or click blank then click option'
Amy_plugin_uninstall
Binit
Cdeactivate_plugin
Dactivate_plugin
Attempts:
3 left
💡 Hint
Common Mistakes
Using activation or deactivation hooks instead of uninstall hook.
Passing a hook name instead of a function name.
2fill in blank
medium

Complete the uninstall function to delete a custom option from the database.

Wordpress
function my_plugin_uninstall() {
    [1]('my_plugin_option');
}
Drag options to blanks, or click blank then click option'
Adelete_option
Bupdate_option
Cadd_option
Dget_option
Attempts:
3 left
💡 Hint
Common Mistakes
Using update_option instead of delete_option.
Trying to get_option instead of deleting.
3fill in blank
hard

Fix the error in the uninstall function to remove a custom database table.

Wordpress
function my_plugin_uninstall() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'my_plugin_data';
    $wpdb->query("[1]");
}
Drag options to blanks, or click blank then click option'
ADELETE TABLE $table_name
BDROP TABLE $table_name
CDROP TABLE IF EXISTS $table_name
DREMOVE TABLE $table_name
Attempts:
3 left
💡 Hint
Common Mistakes
Using DELETE TABLE which is invalid SQL.
Not checking if the table exists before dropping.
4fill in blank
hard

Fill both blanks to remove a custom post type and its metadata during uninstall.

Wordpress
function my_plugin_uninstall() {
    $posts = get_posts(array('post_type' => '[1]', 'numberposts' => -1));
    foreach ($posts as $post) {
        delete_post_meta($post->ID, '[2]');
        wp_delete_post($post->ID, true);
    }
}
Drag options to blanks, or click blank then click option'
Amy_custom_type
Bmy_custom_meta
Cpost_meta_key
Dcustom_post
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong post type name.
Deleting wrong meta key.
5fill in blank
hard

Fill all three blanks to unregister a shortcode and remove its option during uninstall.

Wordpress
function my_plugin_uninstall() {
    [1]('[2]');
    [3]('my_plugin_shortcode_option');
}
Drag options to blanks, or click blank then click option'
Aremove_shortcode
Bdelete_option
Cmy_plugin_shortcode
Dunregister_shortcode
Attempts:
3 left
💡 Hint
Common Mistakes
Using unregister_shortcode which does not exist.
Not deleting the option after removing shortcode.

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

  1. 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.
  2. 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.
  3. Final Answer:

    To clean up plugin data when the plugin is deleted -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    register_uninstall_hook(__FILE__, 'my_cleanup_function'); -> Option D
  4. Quick Check:

    Correct uninstall hook syntax = register_uninstall_hook(__FILE__, 'my_cleanup_function'); [OK]
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

  1. Step 1: Analyze the cleanup function actions

    The function deletes an option, a transient, and drops a custom database table using SQL.
  2. Step 2: Understand uninstall hook behavior

    When the uninstall hook calls this function, all these cleanup actions will run, removing all plugin data.
  3. Final Answer:

    All plugin data including options, transients, and custom tables will be removed -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    The plugin file path should use __FILE__ instead of a string -> Option A
  4. 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

  1. 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.
  2. 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.
  3. 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
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard