📖 Scenario: You want to create a simple WordPress plugin that can be activated and recognized by WordPress.This plugin will have a proper header so WordPress can identify it, and it will run a function when activated.
🎯 Goal: Build a WordPress plugin file with a correct plugin header and an activation hook that runs a function.
📋 What You'll Learn
Create a plugin header with the exact plugin name 'My Sample Plugin'
Add an activation hook that calls a function named 'my_sample_plugin_activate'
Define the 'my_sample_plugin_activate' function with a simple comment inside
Ensure the plugin file is valid PHP with opening tags
💡 Why This Matters
🌍 Real World
WordPress plugins need a proper header and activation hooks to integrate with the WordPress system and perform setup tasks.
💼 Career
Understanding plugin headers and activation hooks is essential for WordPress developers creating custom plugins or maintaining existing ones.
Progress0 / 4 steps
1
Create the plugin header
Create a PHP file starting with <?php and add a plugin header comment block with the exact plugin name My Sample Plugin.
Wordpress
Hint
The plugin header must be a PHP comment block starting with /* and include Plugin Name: My Sample Plugin.
2
Add the activation hook
Add a line that registers an activation hook using register_activation_hook with __FILE__ and the function name my_sample_plugin_activate.
Wordpress
Hint
Use register_activation_hook(__FILE__, 'my_sample_plugin_activate'); to link the activation event to your function.
3
Define the activation function
Define the function my_sample_plugin_activate with an empty body containing only a comment // Activation code here.
Wordpress
Hint
Define the function exactly as function my_sample_plugin_activate() {} and add the comment inside.
4
Complete the plugin file
Ensure the plugin file starts with <?php, includes the plugin header, the activation hook, and the activation function as defined before.
Wordpress
Hint
Check that all parts are present and correctly spelled in the plugin file.
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
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.
Step 2: Differentiate header from activation code
Activation code runs when the plugin is turned on, but the header only provides metadata to WordPress.
Final Answer:
To provide WordPress with information about the plugin like name and version -> Option D
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
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 */ ?>.
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.
Final Answer:
<?php /* Plugin Name: My Plugin */ ?> -> Option B
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?