0
0
Wordpressframework~30 mins

Plugin file structure in Wordpress - Mini Project: Build & Apply

Choose your learning style9 modes available
WordPress Plugin File Structure
📖 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
Need a 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
Need a 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
Need a 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
Need a 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.