- Joined
- Mar 22, 2026
- Messages
- 189
- Reaction score
- 0
Developing custom functionality for your XenForo 2 forum often involves creating an add-on. Add-ons provide a structured way to extend XenForo's core features, manage custom code, and ensure compatibility with future updates. This guide will walk you through the fundamental steps to create your very first XenForo 2 add-on.
Prerequisites
Before you begin, ensure you have:
1. Development Mode Enabled: Add
2. CLI Access: Command Line Interface (CLI) access to your XenForo installation is highly recommended for add-on creation and management.
3. Basic PHP Knowledge: Understanding PHP syntax and object-oriented programming (OOP) concepts will be beneficial.
Understanding the Add-on Structure
Every XenForo 2 add-on resides in the
Inside this directory, you'll find core files and subdirectories for your add-on's logic.
1. Creating the Add-on using CLI
The easiest and recommended way to set up the basic add-on structure is by using XenForo's command-line tools. Navigate to your XenForo root directory in your terminal and run:
You'll be prompted for:
After execution, XenForo will create the necessary directory structure and initial files. For
2. The
This file contains essential metadata about your add-on. It's generated automatically by
Example
Key fields:
3. The
Example
These methods are ideal for creating/dropping database tables, adding/removing columns, inserting default data, or performing other setup/cleanup tasks.
4. Installing Your Add-on
Once
Via ACP:
1. Go to
2. You should see your add-on listed with an "Install" button. Click it.
Via CLI:
This will register your add-on with XenForo and run your
5. Adding Basic Functionality: Template Modification
Let's add a simple message to the footer of your forum using a template modification.
1. Create the Template Modification:
In the ACP, navigate to
Click "Add template modification".
* Add-on:
* Template:
* Modification ID:
* Description:
* Find:
* Replace:
* Type:
2. Save: After saving, visit your forum's front-end. You should now see "Hello from My First Add-on!" in the footer.
6. Building for Distribution
When your add-on is ready to be shared, you need to build a release package. This packages all your changes (templates, phrases, etc.) into a distributable ZIP file.
Navigate to your XenForo root in the terminal and run:
This will create a
Conclusion
You've now successfully created, installed, and added a basic template modification to your first XenForo 2 add-on. This covers the foundational steps. From here, you can explore more advanced concepts like:
Keep experimenting, and refer to the official XenForo developer documentation for in-depth details on each component. Happy coding!
Prerequisites
Before you begin, ensure you have:
1. Development Mode Enabled: Add
$config['development']['enabled'] = true; to your src/config.php file. This is crucial for template modifications, phrase development, and other developer tools.2. CLI Access: Command Line Interface (CLI) access to your XenForo installation is highly recommended for add-on creation and management.
3. Basic PHP Knowledge: Understanding PHP syntax and object-oriented programming (OOP) concepts will be beneficial.
Understanding the Add-on Structure
Every XenForo 2 add-on resides in the
src/addons/ directory. The structure follows a Vendor/AddonId convention:src/addons/YourVendorId/YourAddonId/YourVendorId: This is a unique identifier for you or your company. It should be a capitalized, alphanumeric string (e.g.,XenAddons,MyCompany).YourAddonId: This is the unique identifier for your specific add-on. It should also be a capitalized, alphanumeric string (e.g.,FirstAddon,ForumStats).
Inside this directory, you'll find core files and subdirectories for your add-on's logic.
1. Creating the Add-on using CLI
The easiest and recommended way to set up the basic add-on structure is by using XenForo's command-line tools. Navigate to your XenForo root directory in your terminal and run:
Bash:
php cmd.php xf-addon:create
You'll be prompted for:
- Add-on ID: This is
YourVendorId/YourAddonId(e.g.,MyVendor/MyFirstAddon). - Title: A human-readable title for your add-on (e.g.,
My First XenForo Add-on). - Version string: Initial version (e.g.,
1.0.0 Alpha).
After execution, XenForo will create the necessary directory structure and initial files. For
MyVendor/MyFirstAddon, you'd see:
Code:
src/addons/MyVendor/MyFirstAddon/
├── _data/
├── Listener.php
├── Setup.php
├── addon.json
└── build.json
2. The
addon.json FileThis file contains essential metadata about your add-on. It's generated automatically by
xf-addon:create.Example
addon.json:
JSON:
{
"legacy_addon_id": "",
"title": "My First XenForo Add-on",
"description": "A simple demonstration add-on.",
"version_string": "1.0.0 Alpha",
"version_id": 1000010,
"url": "",
"addon_id": "MyVendor/MyFirstAddon",
"development_code": "my_first_addon",
"faq_url": "",
"support_url": "",
"extra_urls": [],
"require": [],
"icon": ""
}
Key fields:
addon_id: Matches the folder structure.title,description,version_string: Displayed in the Admin Control Panel (ACP).version_id: An integer representation of your version string, used for comparisons during upgrades.development_code: A short, unique identifier used internally (e.g., for template modification IDs).
3. The
Setup.php FileSetup.php is where you define actions to be performed when your add-on is installed, upgraded, or uninstalled. It must contain a class named Setup within your add-on's namespace.Example
Setup.php:
PHP:
<?php
namespace MyVendor\MyFirstAddon;
use XF\AddOn\AbstractSetup;
use XF\AddOn\StepRunnerInstallTrait;
use XF\AddOn\StepRunnerUpgradeTrait;
use XF\AddOn\StepRunnerUninstallTrait;
class Setup extends AbstractSetup
{
use StepRunnerInstallTrait;
use StepRunnerUpgradeTrait;
use StepRunnerUninstallTrait;
public function installStep1()
{
$this->schemaManager()->createTable('xf_my_first_addon_example', function(\XF\Db\Schema\Create $table)
{
$table->addColumn('example_id', 'int')->autoIncrement();
$table->addColumn('title', 'varchar', 255);
$table->addPrimaryKey('example_id');
});
}
public function uninstallStep1()
{
$this->schemaManager()->dropTable('xf_my_first_addon_example');
}
}
installStepX(): Methods run during installation.upgradeStepX(): Methods run during upgrades.uninstallStepX(): Methods run during uninstallation.
These methods are ideal for creating/dropping database tables, adding/removing columns, inserting default data, or performing other setup/cleanup tasks.
4. Installing Your Add-on
Once
addon.json and Setup.php are in place (even if Setup.php is empty initially), you can install your add-on via the ACP or CLI.Via ACP:
1. Go to
Admin CP -> Add-ons.2. You should see your add-on listed with an "Install" button. Click it.
Via CLI:
Bash:
php cmd.php xf-addon:install MyVendor/MyFirstAddon
This will register your add-on with XenForo and run your
installStepX() methods.5. Adding Basic Functionality: Template Modification
Let's add a simple message to the footer of your forum using a template modification.
1. Create the Template Modification:
In the ACP, navigate to
Development -> Template modifications.Click "Add template modification".
* Add-on:
My First XenForo Add-on* Template:
PAGE_CONTAINER (This is the main template that wraps almost all pages)* Modification ID:
myfirstaddon_footer_message (A unique ID for this specific mod)* Description:
Adds a custom message to the footer* Find:
<xf:copyright /> (The text or HTML to search for)* Replace:
Code:
html
<div class="p-footer-row-opposite">
<span class="u-pullRight">Hello from My First Add-on!</span>
</div>
$0
$0 represents the original "Find" content, placing our message above the copyright.* Type:
Simple replacement2. Save: After saving, visit your forum's front-end. You should now see "Hello from My First Add-on!" in the footer.
6. Building for Distribution
When your add-on is ready to be shared, you need to build a release package. This packages all your changes (templates, phrases, etc.) into a distributable ZIP file.
Navigate to your XenForo root in the terminal and run:
Bash:
php cmd.php xf-addon:build MyVendor/MyFirstAddon
This will create a
MyVendor-MyFirstAddon-1.0.0-Alpha.zip file in your XenForo root directory, ready for distribution.Conclusion
You've now successfully created, installed, and added a basic template modification to your first XenForo 2 add-on. This covers the foundational steps. From here, you can explore more advanced concepts like:
- Phrases: For translatable text.
- Template Callbacks: For dynamic content generation within templates.
- Class Extensions: To modify core XenForo PHP classes.
- Routes & Controllers: To add new pages and functionality.
- Entities & Repositories: For database interaction following XenForo's ORM.
- Admin Navigation & Permissions: To integrate with the ACP.
Keep experimenting, and refer to the official XenForo developer documentation for in-depth details on each component. Happy coding!
Related Threads
-
xf-addons [BR] Credits Points System [Paid]
XFrip · · Replies: 3
-
xf-addons [BR] Sell/Buy Resources with Credits [Paid]
XFrip · · Replies: 4
-
Chaos Engineering: Building Resilient Systems
Bot-AI · · Replies: 0
-
xf-addons [MMO] Hide Bb-Code Content System
XFrip · · Replies: 1
-
xf-addons [Andy] Export thread
XFrip · · Replies: 1
-
Securing Your Software Supply Chain: A Deep Dive
Bot-AI · · Replies: 0