Add a custom action

By the end of this article, you should be able to confidently create a basic action on your forum.

What is an action?
An action is a page off the base of your forum. It is comprised of two files, one is a source file, and the second is a template file.

Source File
The source file is the base foundation of the action, it loads the template along with anything else the action might need, such as permissions or additional functions.

Template File
The template file is everything that you see when you go to ?action=youraction, it is defined to the action by the actions source file.

Now that we have a basic understanding of how actions work, lets begin!

First of all, we need to define the action along with the source file name and function.

/index.php
Find 'xmlhttp' => array('Xml.php', 'XMLhttpMain'), Add After // Your Custom Action 'youraction' => array('YourAction.php', 'YourActionMain'),

[hr]Notes about the first edit: the array that defines actions inside index.php is alphabetically ordered, to keep everything organized, it is smart to label what the code you added was for, and also keep it alphabetically labeled. [quote]'action-in-url' => array('Source-File.php', 'FunctionToCall'),[/quote][hr]

Now that we have defined our action, lets create our Source file.

Create a file in ./Sources named "YourAction.php".

./Sources/YourAction.php
'Add  $scripturl. '?action=youraction',  		'name' => $txt['your_action'], 	); } ?>

Notes about the source file: first of all, we make sure we are accessing the source file via SMF so that people can not directly access the file. Then, we globalize the variables we need for the main function, load our template, define our page title and our link tree item

Now that we have our action defined, and a source file for our base, lets work on our template file.

Go to ./Themes/default and create a file named "YourAction.template.php".

Once created, complete the following edit.

./Themes/default/YourAction.template.php
Add

Now our action should be accessible and working correctly, except no text will be showing, so lets define the strings we used.

./Themes/default/languages/Modifications.english.php
Add Before End

// Your Action Language Strings $txt['your_action'] = 'Your Action'; $txt['page_title'] = 'This is your page title';

The final edit is very simple to do.

./Themes/default/languages/Who.english.php
Add Before End

// Your Action Who String $txt['whoall_youraction'] = 'Viewing Your Action.';

The edit you make in Who.english.php defines the action for the ?action=who page. So people can see what action you are on.

Another optional edit would be to add a navigation tab for it. Take a peak at the document on adding tabs to do that.

Also remember to clear your forums cache or the text strings will not show up.