Buttons - How do I add buttons to SMF 2.0

From Online Manual

Jump to: navigation, search

The following instructions describe how to add a link to the main menu, which will appear just before the members list. To place the menu item in a different order, find the appropriately named section of the array and add the following code before it.

In /Sources/Subs.php find:

'mlist' => array(

add before:

'menu_action' => array(
				'title' => 'text for menu', 
				'href' => $scripturl,
				'show' => true,
				'sub_buttons' => array(
					'sub_menu_action' => array(
						'title' => 'text for sub-menu',
						'href' => $scripturl . '?action=newaction',
						'show' => true,
						'is_last'=> true,
					),
				),
			),

As you can see, each entry in the array has a main section and a sub_button(s) section. Each of the items in the main level of the array must be present and defined.

  • The menu_action is a standard PHP variable name (no spaces) that clearly and concisely describes the action.
  • The title is the text that will be shown in the main menu. If the forum uses only a single language, this can be defined directly in the code (hardcoded). However, if this is being coded for a mod, or the forum uses more than one language, the text for this item should use a $txt string variable which is then defined in the modifications.each_language.php file(s).
  • The href is the link that the menu item will link to. This can be a relative URL, an absolute URL, or, as in this case, a URL defined by the standard $scripturl global variable.
  • The show variable defines whether this menu item is displayed. This can be set to true, false, or defined to use some sort of logic (i.e. allowedTo('admin_forum')).
  • If there are no further drop-down/sub-level buttons for this menu item, then sub_buttons can be defined as an empty sub-array. i.e.
'sub_buttons' => array(
				),
  • However, if the menu item is going to have sub-menu items, then each sub-menu action needs to be defined under sub_buttons.
    • The sub_menu_action is a standard PHP variable name (no spaces) that clearly and concisely describes the sub-action.
    • The title is the text that will be shown in the sub-menu. Like the main menu tile-text, if the forum uses only a single language, this can be defined directly in the code (hardcoded). However, if this is being coded for a mod, or the forum uses more than one language, the text for this item should use a $txt string variable which is then defined in the modifications.each_language.php file(s).
    • The href is the link that the sub-menu item will link to. Again, this can be a relative URL, an absolute URL, or, as in this case, a URL defined by the standard $scripturl global variable, along with an action (which must exist in the index.php action array).
    • The show variable defines whether this sub-menu item is displayed. This can be set to true, false, or defined to use some sort of logic (i.e. allowedTo('admin_forum')).
    • If this is the last sub-item in the sub-menu, then the line 'is_last'=true; should be added/defined in order for the CSS to properly close the dropdown list.

Lastly, for correct coding, each of of the sub-arrays which was opened must be closed. i.e. for each opening bracket, there must also be a closing bracket.

Text Strings

'title' => 'text for menu',

In the above example, the text between the apostrophes is the name of the button. It is not an issue to name your buttons like this, providing your forum uses only a single language, but it is not recommended. If you do use multiple languages or wish to follow the standard coding practice that the other buttons use, you will define the text of the button as a text string. If you would like to follow the default buttons' style and use text strings, here is how:

  • Open Modifications.YourLangauge.php in Themes > Default > Languages.
  • Before the last line add your string like so:
$txt['new_button'] = 'My New Button';
  • Replace 'text for menu' with $txt['new_button'] so it looks like:
'title' => $txt['new_button'],

Now the button name in the main menu will be "My New Button".

See also

Adding tabs to SMF 2.0