Add a custom action: Difference between revisions From Online Manual

Jump to: navigation, search
(added code template, once)
(code template added few links)
Line 1: Line 1:
{{Merge|Creating a custom action}}
{{TOCright}}
By the end of this article, you should be able to confidently create a basic action on your forum.
By the end of this article, you should be able to confidently create a basic action on your forum.


Line 9: Line 9:


'''Find'''
'''Find'''
    <nowiki>'xmlhttp' => array('Xml.php', 'XMLhttpMain'),</nowiki>
{{code|1=<nowiki>'xmlhttp' => array('Xml.php', 'XMLhttpMain'),</nowiki>}}
'''Add After'''
'''Add After'''
  <nowiki>// Your Custom Action
{{code|1=<nowiki> // Your Custom Action
  'youraction' => array('YourAction.php', 'YourActionMain'),</nowiki>
'youraction' => array('YourAction.php', 'YourActionMain'),</nowiki>}}
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]
 
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.
{{code|1=<nowiki>'action-in-url' => array('Source-File.php', 'FunctionToCall'),</nowiki>}}


===Source File===
===Source File===
Line 22: Line 24:
=====./Sources/YourAction.php=====
=====./Sources/YourAction.php=====
'''Add'''
'''Add'''
{{code|1=<nowiki><?php
{{code|1=<nowiki><?php
 
if (!defined('SMF'))
if (!defined('SMF'))
die('Hack Attempt...');
die('Hack Attempt...');
 
function YourActionMain()
function YourActionMain()
{
{
 
// Globalize what we need...
// Globalize what we need...
global $context, $scripturl, $txt;
global $context, $scripturl, $txt;
 
// Load our template
// Load our template
loadTemplate('YourAction');
loadTemplate('YourAction');
 
// Define a page title
// Define a page title
$context['page_title'] = $txt['page_title'];
$context['page_title'] = $txt['page_title'];
 
// Define our link tree
// Define our link tree
$context['linktree'][] = array(
$context['linktree'][] = array(
  'url' => $scripturl. '?action=youraction',
  'url' => $scripturl. '?action=youraction',
  'name' => $txt['your_action'],
'name' => $txt['your_action'],
);
);
 
}
}
 
?></nowiki>}}
?></nowiki>}}
Notes: 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
Notes: 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


Line 59: Line 61:
====./Themes/default/YourAction.template.php====
====./Themes/default/YourAction.template.php====
'''Add'''
'''Add'''
<nowiki><?php
{{code|1=<nowiki><?php
 
function template_main()
function template_main()
{
{
 
// Globalize what we need...
// Globalize what we need...
global $txt;
global $txt;
 
// Catbg header
// Catbg header
echo '<div class="cat_bar">
echo '<div class="cat_bar">
    <h3 class="catbg">', $txt['your_action'], '</h3>
  <h3 class="catbg">', $txt['your_action'], '</h3>
</div>';
</div>';
 
// Windowbg2 Content
// Windowbg2 Content
echo '<div class="windowbg2">
echo '<div class="windowbg2">
  <span class="topslice"><span></span></span>
  <span class="topslice"><span></span></span>
    <div class="content">Your Page Content</div>
    <div class="content">Your Page Content</div>
  <span class="botslice"><span></span></span>
  <span class="botslice"><span></span></span>
</div><br />';
</div><br />';
 
}
}
 
?></nowiki>
?></nowiki>}}


===Language Strings===
===Language Strings===
Line 88: Line 90:
====./Themes/default/languages/Modifications.english.php====
====./Themes/default/languages/Modifications.english.php====
'''Add Before End'''
'''Add Before End'''
<nowiki>// Your Action Language Strings
{{code|1=<nowiki>// Your Action Language Strings
$txt['your_action'] = 'Your Action';
$txt['your_action'] = 'Your Action';
$txt['page_title'] = 'This is your page title';</nowiki>
$txt['page_title'] = 'This is your page title';</nowiki>}}


====./Themes/default/languages/Who.english.php====
====./Themes/default/languages/Who.english.php====
'''Add Before End'''
'''Add Before End'''
<nowiki>// Your Action Who String
{{code|1=<nowiki>// Your Action Who String
$txt['whoall_youraction'] = 'Viewing <a href="' . $scripturl . '?action=youraction">Your Action</a>.';</nowiki>
$txt['whoall_youraction'] = 'Viewing <a href="' . $scripturl . '?action=youraction">Your Action</a>.';</nowiki>}}


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.
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.
Line 103: Line 105:


Another optional edit would be to add a navigation tab for it. Take a peak at the document on adding tabs to do that.
Another optional edit would be to add a navigation tab for it. Take a peak at the document on adding tabs to do that.
===Alternative methods===
[[Add a custom action using integration_hooks]]


[[Category:Tips and tricks]]
[[Category:Tips and tricks]]
[[Category:Developing SMF]]
[[Category:Developing Mods]]

Revision as of 23:01, 7 May 2011

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.

/index.php

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

Find

'xmlhttp' => array('Xml.php', 'XMLhttpMain'),

Add After

	// Your Custom Action
	'youraction' => array('YourAction.php', 'YourActionMain'),

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.

'action-in-url' => array('Source-File.php', 'FunctionToCall'),

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.

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

./Sources/YourAction.php

Add

<?php

if (!defined('SMF'))
	die('Hack Attempt...');

function YourActionMain()
{

	// Globalize what we need...
	global $context, $scripturl, $txt;

	// Load our template
	loadTemplate('YourAction');

	// Define a page title
	$context['page_title'] = $txt['page_title'];

	// Define our link tree
	$context['linktree'][] = array(
  		'url' => $scripturl. '?action=youraction',
 		'name' => $txt['your_action'],
	);

}

?>

Notes: 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

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.

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

Once created, complete the following edit.

./Themes/default/YourAction.template.php

Add

<?php

function template_main()
{

	// Globalize what we need...
	global $txt;

	// Catbg header
	echo '<div class="cat_bar">
   			<h3 class="catbg">', $txt['your_action'], '</h3>
	</div>';

	// Windowbg2 Content
	echo '<div class="windowbg2">
  			<span class="topslice"><span></span></span>
  		  		<div class="content">Your Page Content</div>
  			<span class="botslice"><span></span></span>
	</div><br />';

}

?>

Language Strings

The language strings define text in both the template file, and the source file so they can be edited more easily and also translated to other languages more easily.

./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';

./Themes/default/languages/Who.english.php

Add Before End

// Your Action Who String
$txt['whoall_youraction'] = 'Viewing <a href="' . $scripturl . '?action=youraction">Your Action</a>.';

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.

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


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

Alternative methods

Add a custom action using integration_hooks



Advertisement: