Bird
Raised Fist0
Wordpressframework~20 mins

Plugin header and activation in Wordpress - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
WordPress Plugin Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when a WordPress plugin with this header is activated?
Consider this plugin header in a PHP file placed in the WordPress plugins folder. What will WordPress do when this plugin is activated?
/*
Plugin Name: Simple Logger
Description: Logs a message on activation.
Version: 1.0
Author: Dev
*/

register_activation_hook(__FILE__, function() {
    error_log('Simple Logger activated');
});
Wordpress
/*
Plugin Name: Simple Logger
Description: Logs a message on activation.
Version: 1.0
Author: Dev
*/

register_activation_hook(__FILE__, function() {
    error_log('Simple Logger activated');
});
AWordPress will display 'Simple Logger activated' as a message on the admin dashboard after activation.
BWordPress will log 'Simple Logger activated' to the PHP error log when the plugin is activated.
CThe plugin will fail to activate because register_activation_hook cannot be used inside the plugin file.
DNothing happens because the activation hook is not properly registered.
Attempts:
2 left
💡 Hint
Think about what register_activation_hook does and where it runs.
📝 Syntax
intermediate
1:30remaining
Identify the correct plugin header syntax
Which of the following plugin headers is correctly formatted so WordPress recognizes it as a valid plugin?
A
/*
Plugin Name: My Plugin
Description: Does something useful
Version: 1.0
Author: Me
*/
B
// Plugin Name: My Plugin
// Description: Does something useful
// Version: 1.0
// Author: Me
C
<!-- Plugin Name: My Plugin -->
<!-- Description: Does something useful -->
<!-- Version: 1.0 -->
<!-- Author: Me -->
D
# Plugin Name: My Plugin
# Description: Does something useful
# Version: 1.0
# Author: Me
Attempts:
2 left
💡 Hint
WordPress plugin headers must be inside a PHP comment block at the top of the main plugin file.
🔧 Debug
advanced
2:30remaining
Why does this activation hook not run?
A developer wrote this plugin code but the activation hook never runs. Why?
/*
Plugin Name: Test Plugin
*/

function activate_test_plugin() {
    error_log('Activated');
}

register_activation_hook('test-plugin.php', 'activate_test_plugin');
Wordpress
/*
Plugin Name: Test Plugin
*/

function activate_test_plugin() {
    error_log('Activated');
}

register_activation_hook('test-plugin.php', 'activate_test_plugin');
AThe function activate_test_plugin is missing a required parameter.
BThe plugin header is missing the Version field, so activation hooks don't run.
CThe error_log function cannot be used inside activation hooks.
DThe first argument to register_activation_hook must be __FILE__, not a string filename.
Attempts:
2 left
💡 Hint
Check the first argument of register_activation_hook carefully.
❓ state_output
advanced
2:00remaining
What is the output after plugin activation and deactivation?
Given this plugin code, what will be logged after activating and then deactivating the plugin?
/*
Plugin Name: Logger Plugin
*/

register_activation_hook(__FILE__, function() {
    error_log('Plugin activated');
});

register_deactivation_hook(__FILE__, function() {
    error_log('Plugin deactivated');
});
Wordpress
/*
Plugin Name: Logger Plugin
*/

register_activation_hook(__FILE__, function() {
    error_log('Plugin activated');
});

register_deactivation_hook(__FILE__, function() {
    error_log('Plugin deactivated');
});
AOnly 'Plugin activated' will be logged; deactivation hook does not run automatically.
BNothing will be logged because error_log is disabled in WordPress plugins.
CThe PHP error log will contain 'Plugin activated' after activation and 'Plugin deactivated' after deactivation.
DBoth hooks run immediately when the plugin file loads, logging both messages at once.
Attempts:
2 left
💡 Hint
Activation and deactivation hooks run only on those specific events.
🧠 Conceptual
expert
3:00remaining
Why is the plugin header required for activation?
Why does WordPress require a properly formatted plugin header comment block at the top of a plugin file before it can be activated?
ABecause WordPress uses the header to identify the plugin's name, version, and author to list it in the admin plugins page.
BBecause the header comment block contains PHP code that runs the activation hook automatically.
CBecause without the header, WordPress cannot load the plugin's PHP functions into memory.
DBecause the header comment block is where WordPress stores the plugin's database connection details.
Attempts:
2 left
💡 Hint
Think about how WordPress shows plugins in the admin area.

Practice

(1/5)
1. What is the main purpose of the plugin header in a WordPress plugin?
easy
A. To style the plugin's admin page
B. To run code when the plugin is activated
C. To connect the plugin to the WordPress database
D. To provide WordPress with information about the plugin like name and version

Solution

  1. Step 1: Understand the role of the plugin header

    The plugin header is a comment block at the top of the main plugin file that tells WordPress details like the plugin's name, version, author, and description.
  2. Step 2: Differentiate header from activation code

    Activation code runs when the plugin is turned on, but the header only provides metadata to WordPress.
  3. Final Answer:

    To provide WordPress with information about the plugin like name and version -> Option D
  4. Quick Check:

    Plugin header = plugin info [OK]
Hint: Plugin header is metadata, not code execution [OK]
Common Mistakes:
  • Confusing header with activation hook
  • Thinking header runs code
  • Assuming header styles plugin pages
2. Which of the following is the correct way to start a plugin header in a WordPress plugin file?
easy
A.
B.
C. /*\nPlugin Name: My Plugin\n*/
D. "

Solution

  1. Step 1: Recognize the plugin header format

    The plugin header must be a PHP comment block at the top of the main plugin file, starting with and ending with */ ?>.
  2. Step 2: Check each option's syntax

    uses the correct PHP comment block format. uses a single-line comment which WordPress does not recognize as a header. /*\nPlugin Name: My Plugin\n*/ is a string, not a comment. " is an HTML comment, which WordPress ignores.
  3. Final Answer:

    <?php /* Plugin Name: My Plugin */ ?> -> Option B
  4. Quick Check:

    Plugin header = PHP comment block [OK]
Hint: Plugin header must be inside PHP comment block [OK]
Common Mistakes:
  • Using single-line comments for header
  • Using HTML comments instead of PHP comments
  • Not starting with php
3. Given this plugin activation code snippet, what will happen when the plugin is activated?
register_activation_hook(__FILE__, function() {
    update_option('my_plugin_activated', true);
});
medium
A. The option 'my_plugin_activated' will be set to true in the database
B. Nothing happens because the hook is incorrect
C. The plugin will deactivate immediately
D. An error will occur because update_option is not allowed

Solution

  1. Step 1: Understand register_activation_hook usage

    This function runs the given callback once when the plugin is activated.
  2. Step 2: Analyze the callback function

    The callback calls update_option to set 'my_plugin_activated' to true in the WordPress options table.
  3. Final Answer:

    The option 'my_plugin_activated' will be set to true in the database -> Option A
  4. Quick Check:

    Activation hook runs callback once [OK]
Hint: Activation hook runs code once on plugin activation [OK]
Common Mistakes:
  • Thinking activation hook runs on every page load
  • Confusing activation with deactivation hooks
  • Assuming update_option causes errors
4. Identify the error in this plugin activation code:
register_activation_hook(__FILE__, 'activate_plugin');

function activate_plugin() {
    add_option('plugin_activated', true);
}
medium
A. Using add_option instead of update_option causes failure if option exists
B. register_activation_hook must use an anonymous function, not named
C. The function activate_plugin is missing a return statement
D. The __FILE__ constant is not allowed in register_activation_hook

Solution

  1. Step 1: Understand add_option vs update_option

    add_option adds a new option only if it does not exist. If the option exists, it fails silently. update_option adds or updates the option.
  2. Step 2: Check the activation code behavior

    If the plugin is activated multiple times or the option already exists, add_option will not update the value, possibly causing unexpected behavior.
  3. Final Answer:

    Using add_option instead of update_option causes failure if option exists -> Option A
  4. Quick Check:

    Use update_option to ensure option is set [OK]
Hint: Use update_option in activation to avoid silent failures [OK]
Common Mistakes:
  • Confusing add_option and update_option
  • Thinking named functions can't be used
  • Misunderstanding __FILE__ usage
5. You want your plugin to create a custom database table only once when activated. Which is the best way to do this in your plugin activation hook?
hard
A. Create the table inside the plugin's main file without any condition
B. Create the table every time the plugin runs, outside any hook
C. Use register_activation_hook with a function that checks if the table exists, then creates it if missing
D. Use the plugin header to define the table creation SQL

Solution

  1. Step 1: Understand when to create database tables

    Database tables should be created once on plugin activation to avoid errors and performance issues.
  2. Step 2: Use activation hook with existence check

    The activation hook runs once. Checking if the table exists before creating prevents errors if the plugin is reactivated.
  3. Step 3: Avoid creating tables on every page load or in headers

    Creating tables outside activation or without checks can cause repeated attempts and errors.
  4. Final Answer:

    Use register_activation_hook with a function that checks if the table exists, then creates it if missing -> Option C
  5. Quick Check:

    Activation hook + check = safe table creation [OK]
Hint: Create tables only once inside activation hook with checks [OK]
Common Mistakes:
  • Creating tables on every page load
  • Putting SQL in plugin header
  • Not checking if table exists before creating
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