Bird
Raised Fist0
Wordpressframework~10 mins

Plugin file structure in Wordpress - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Plugin file structure
Create plugin folder
↓
Add main plugin file
↓
Add plugin header comment
↓
Add additional files (optional)
↓
Activate plugin in WordPress
↓
Plugin runs and hooks into WordPress
This flow shows how a WordPress plugin is structured starting from folder creation to activation and running inside WordPress.
Execution Sample
Wordpress
<?php
/*
Plugin Name: My Plugin
*/
// Plugin code here
This is the main plugin file with the required header comment that WordPress reads to identify the plugin.
Execution Table
StepActionFile/Folder CreatedContent SummaryWordPress Reaction
1Create plugin foldermy-plugin/Empty folder to hold plugin filesRecognizes folder as plugin container
2Add main plugin filemy-plugin/my-plugin.phpPHP fileRecognizes main plugin file
3Add plugin header commentmy-plugin/my-plugin.phpContains Plugin Name and infoPlugin appears in admin plugins list
4Add additional filesmy-plugin/includes/functions.phpOptional PHP files for organizationFiles loaded by main plugin file if included
5Activate pluginWordPress adminUser activates plugin in dashboardPlugin code runs and hooks into WordPress
6Plugin runsN/AHooks and functions executePlugin functionality is available on site
💡 Plugin is fully loaded and active after activation step
Variable Tracker
Variable/FileStartAfter Step 2After Step 4After Activation
Plugin folderNot createdCreatedExistsExists
Main plugin fileNot createdCreatedExists with headerExists and loaded
Additional filesNoneNoneAddedLoaded if included
Plugin active stateInactiveInactiveInactiveActive
Key Moments - 3 Insights
Why is the plugin header comment important?
WordPress reads the header comment in the main plugin file to identify the plugin and show it in the admin list. Without it, WordPress ignores the plugin folder (see execution_table step 3).
Can a plugin have multiple PHP files?
Yes, additional PHP files can be added for better organization, but the main plugin file must include or require them to run (see execution_table step 4).
What happens if the plugin is not activated?
The plugin files exist but WordPress does not run the plugin code or hooks until the user activates it in the admin dashboard (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does WordPress recognize the plugin metadata?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Check the 'WordPress Reaction' column for when the plugin appears in the admin list.
According to the variable tracker, when does the plugin become active?
AAfter Step 2
BAfter Step 4
CAfter Activation
DAt Start
💡 Hint
Look at the 'Plugin active state' row in the variable tracker.
If you add a new PHP file but do not include it in the main plugin file, what happens?
AThe file is ignored and code inside does not run
BWordPress loads it automatically
CPlugin fails to activate
DPlugin header comment is ignored
💡 Hint
Refer to key moment about additional files and execution_table step 4.
Concept Snapshot
WordPress Plugin File Structure:
- Create a plugin folder inside wp-content/plugins
- Add a main PHP file with a plugin header comment
- Optionally add more PHP files for organization
- Activate plugin in WordPress admin
- Plugin code runs and hooks into WordPress
Header comment is required for WordPress to detect the plugin.
Full Transcript
A WordPress plugin starts as a folder inside the plugins directory. Inside this folder, you create a main PHP file that must include a special header comment with the plugin name and info. This header allows WordPress to recognize and list the plugin in the admin dashboard. You can add more PHP files for organizing your code, but these must be included by the main plugin file to run. The plugin remains inactive until you activate it in the WordPress admin. Once activated, WordPress runs the plugin code and hooks, making the plugin's features available on the site.

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

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

    A PHP file with plugin header comments -> Option A
  4. 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

  1. 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.
  2. Step 2: Evaluate other options

    Putting all files in root or a single folder makes the plugin messy. Having no folders limits scalability.
  3. Final Answer:

    Separate PHP files into a 'includes' folder and assets into 'assets' folder -> Option D
  4. Quick Check:

    Organize by type in folders = B [OK]
Hint: Use 'includes' for PHP and 'assets' for CSS/JS [OK]
Common Mistakes:
  • Mixing all files in root folder
  • Putting everything inside 'assets' folder
  • Not using folders at all
3. Given this plugin folder structure:
my-plugin/
  my-plugin.php
  includes/
    functions.php
  assets/
    style.css
    script.js

Which file should contain the plugin header comments?
medium
A. includes/functions.php
B. my-plugin.php
C. assets/script.js
D. assets/style.css

Solution

  1. Step 1: Identify the main plugin file

    The main plugin file is the one in the root folder named after the plugin, here 'my-plugin.php'. This file must have the plugin header comments.
  2. Step 2: Understand roles of other files

    Files in 'includes' and 'assets' folders hold supporting code and styles/scripts but do not contain the plugin header.
  3. Final Answer:

    my-plugin.php -> Option B
  4. Quick Check:

    Main plugin file = my-plugin.php [OK]
Hint: Plugin header is in root PHP file named after plugin [OK]
Common Mistakes:
  • Putting header in includes/functions.php
  • Thinking CSS or JS files hold plugin info
  • Confusing folder files with main plugin file
4. You see this plugin structure:
plugin-folder/
  plugin-folder.php
  includes.php
  assets/
    style.css

What is wrong with this structure?
medium
A. The 'assets' folder should be named 'styles'
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

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

    The 'includes.php' file should be a folder named 'includes' -> Option C
  4. 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

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

    Add a 'languages' folder for translation files and keep main PHP file in root -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard