📖 Scenario: You want to create a simple WordPress plugin that adds a custom greeting message to your site. To do this, you need to set up the correct plugin file structure so WordPress can recognize and activate it.
🎯 Goal: Build the basic file structure for a WordPress plugin with the main plugin file containing the plugin header comment.
📋 What You'll Learn
Create the main plugin PHP file with the exact plugin header comment
Add a subfolder named includes inside the plugin folder
Create a PHP file inside includes named greeting-functions.php
Include the greeting-functions.php file in the main plugin file using require_once
💡 Why This Matters
🌍 Real World
WordPress plugins extend site functionality. Knowing the correct file structure helps you build plugins that WordPress can load and manage easily.
💼 Career
Many web development jobs require creating or maintaining WordPress plugins. Understanding plugin structure is essential for these roles.
Progress0 / 4 steps
1
Create the main plugin file with header
Create a PHP file named simple-greeting.php with the exact plugin header comment shown below at the top of the file:
/*
Plugin Name: Simple Greeting
Description: Adds a greeting message to your site.
Version: 1.0
Author: Your Name
*/
Wordpress
Hint
The plugin header comment must be at the very top of the main PHP file and start with /* and end with */.
2
Add includes folder and greeting-functions.php file
Inside your plugin folder, create a subfolder named includes. Then create a PHP file named greeting-functions.php inside the includes folder. This file will hold your greeting functions.
Wordpress
Hint
Folders are not code, but you must create the includes folder and inside it the greeting-functions.php file.
3
Include greeting-functions.php in main plugin file
In the simple-greeting.php file, add a line to include the greeting-functions.php file from the includes folder using require_once and the plugin_dir_path(__FILE__) function.
Wordpress
Hint
Use require_once plugin_dir_path(__FILE__) . 'includes/greeting-functions.php'; to include the file.
4
Add a simple greeting function in greeting-functions.php
In the includes/greeting-functions.php file, create a function named simple_greeting_message that returns the string 'Hello, welcome to my site!'. Then, in the main plugin file simple-greeting.php, add a WordPress action hook to display this greeting in the site footer using add_action with the hook wp_footer.
Wordpress
Hint
Define the function in greeting-functions.php and use an anonymous function with add_action in the main plugin file to echo the greeting.
Practice
(1/5)
1. Which file is required as the main entry point for a WordPress plugin?
easy
A. A PHP file with plugin header comments
B. A CSS file for styling the plugin
C. A JavaScript file for plugin scripts
D. An image file for the plugin icon
Solution
Step 1: Understand the main plugin file role
The main plugin file must be a PHP file that contains header comments describing the plugin. This file acts as the entry point for WordPress to recognize and load the plugin.
Step 2: Differentiate file types in plugin structure
CSS, JavaScript, and image files are used for styling, scripting, and visuals but are not the main plugin file.
Final Answer:
A PHP file with plugin header comments -> Option A
Quick Check:
Main plugin file = PHP with header [OK]
Hint: Main plugin file always has PHP header comments [OK]
Common Mistakes:
Choosing CSS or JS files as main plugin file
Thinking image files can be main plugin file
Ignoring the required plugin header comments
2. Which of the following is the correct way to organize plugin files for better structure?
easy
A. Put all PHP, CSS, and JS files in the root plugin folder
B. Keep only the main PHP file and no folders
C. Place all files inside a single 'assets' folder
D. Separate PHP files into a 'includes' folder and assets into 'assets' folder
Solution
Step 1: Understand folder organization best practices
Separating PHP files into an 'includes' folder and assets like CSS and JS into an 'assets' folder keeps the plugin organized and easier to maintain.
Step 2: Evaluate other options
Putting all files in root or a single folder makes the plugin messy. Having no folders limits scalability.
Final Answer:
Separate PHP files into a 'includes' folder and assets into 'assets' folder -> Option D
Quick Check:
Organize by type in folders = B [OK]
Hint: Use 'includes' for PHP and 'assets' for CSS/JS [OK]
B. The main plugin file should be named differently
C. The 'includes.php' file should be a folder named 'includes'
D. There should be no CSS files in the plugin
Solution
Step 1: Check folder vs file naming conventions
Typically, 'includes' is a folder holding PHP files, not a single PHP file named 'includes.php'. This breaks organization.
Step 2: Validate other options
The main plugin file name is fine if it matches the folder. 'assets' is a common folder name for CSS/JS. CSS files are allowed.
Final Answer:
The 'includes.php' file should be a folder named 'includes' -> Option C
Quick Check:
'includes' must be a folder, not a file [OK]
Hint: 'includes' is a folder, not a single PHP file [OK]
Common Mistakes:
Renaming main plugin file unnecessarily
Renaming 'assets' folder incorrectly
Thinking CSS files are not allowed
5. You want to add language support to your plugin. Which is the best way to organize your plugin files?
hard
A. Add a 'languages' folder for translation files and keep main PHP file in root
B. Put translation files inside the 'assets' folder with CSS and JS
C. Include translation files directly inside the main PHP file
D. Create a separate plugin just for translations
Solution
Step 1: Understand language file organization
WordPress plugins commonly use a 'languages' folder to store translation files (.mo, .po). This keeps translations separate and organized.
Step 2: Evaluate other options
Putting translations in 'assets' mixes code and assets. Including translations in PHP file is not practical. Separate plugin for translations is unnecessary.
Final Answer:
Add a 'languages' folder for translation files and keep main PHP file in root -> Option A
Quick Check:
Use 'languages' folder for translations [OK]
Hint: Use 'languages' folder for translations, not 'assets' [OK]
Common Mistakes:
Mixing translation files with CSS/JS
Embedding translations inside PHP code
Creating separate plugin for translations
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.