Advertisement:

Author Topic: Adding tabs to SMF 2.0  (Read 78885 times)

Offline H

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 21,659
  • Gender: Male
Adding tabs to SMF 2.0
« on: September 14, 2008, 11:24:35 AM »
In this tutorial you will learn how to add tabs to SMF2. Unlike SMF 1.0.x and 1.1.x the menu has moved from being in the templates to being in one of the SMF source files so that it can be shared between themes.
This makes it easier for you to add a tab to all themes at once and mean there will be less work to do when manually installing mods on custom themes.

In order for this to work your theme will need to have already been upgraded for 2.0 :)

Firstly you'll need to open Sources / Subs.php

Scroll down to:

Code: [Select]
// All the buttons we can possible want and then some, try pulling the final list of buttons from cache first.
This is where the tabs are set.

Each button is an entry in a big array. Here is the first button:

Code: [Select]
'home' => array(
'title' => $txt['home'],
'href' => $scripturl,
'show' => true,
'sub_buttons' => array(
),
),

To add a button copy and paste this code between the buttons where you want this button to be located.
Change 'home' to something simple and unique
Change $txt['home'] to the name of your button in quotes
Change $scripturl to the location of your button in quotes
Change show to true (you can do more advanced things with this which will be explained later in the tutorial)
Ignore sub-buttons for now. This can create a drop-down menu and will also be covered later in the tutorial

My example button would look like this, inbetween home and help:

Code: [Select]
'home' => array(
'title' => $txt['home'],
'href' => $scripturl,
'show' => true,
'sub_buttons' => array(
),
),
'arcade' => array(
'title' => 'Arcade',
'href' => 'http://www.mysite.example/arcade',
'show' => true,
'sub_buttons' => array(
),
),
'help' => array(
'title' => $txt['help'],
'href' => $scripturl . '?action=help',
'show' => true,
'sub_buttons' => array(
),
),

Now you just need to save the file and upload with FTP if necessary. You may want to read How do I use FTP? / What is FTP?.
Finally if you use the SMF cache you'll need to clear it by going to the Admin area > Forum Maintenance > Empty the file cache.

If you just want a simple button you can stop reading now, otherwise read on!

Note that if you wish to add a button at the end of the menu it must be placed after the logout button and (after logout / register) you'll need to change is_last to false in login and register:

Code: [Select]
'is_last' => true,
You also need to add it to your button after :

Code: [Select]
'sub_buttons' => array(
),

Which would make something like this:

Code: [Select]
'arcade' => array(
'title' => 'Arcade',
'href' => 'http://www.mysite.example/arcade',
'show' => true,
'sub_buttons' => array(
),
'is_last' => true,

),



Adding buttons in a multiple language forum

When adding a button I suggested that you replace $txt['home'] with the name that you wanted for your button. This works well until you have a forum with more than one language. Then you'll need to use the language files so that the text can be translated.

Instead of doing this:

Code: [Select]
'title' => 'Arcade',
You'll need to do something like:

Code: [Select]
'title' => $txt['arcade_button'],
Make sure you use a fairly unique name as this will ensure it isn't the same as some text that SMF is already using. Then you'll need to open each index.language.php file in Themes / default / languages (obviously replace language with the name of your language). At the bottom of the file add:

Code: [Select]
$txt['arcade_button'] = 'Arcade';
Make sure you do the same in every language's file.

Showing buttons only for logged in users

You may wish to have a button that is only displayed for logged in users. You can do this by changing:

Code: [Select]
'show' => true,
If you want a button that is only displayed for guests change to:

Code: [Select]
'show' => $user_info['is_guest'],
If you want a button that is only displayed for logged in users:

Code: [Select]
'show' => !$user_info['is_guest'],
If you want a button that is only displayed for admins:

Code: [Select]
'show' => $context['user']['is_admin']
Advanced users can also look at the allowedTo() function to restrict based on permissions

Sub-buttons

Please note that not all themes will support sub-buttons :)

You can set sub-buttons which will be displayed on the drop-down menus. Take a look at the code used for the calendar menu:

Code: [Select]
'calendar' => array(
'title' => $txt['calendar'],
'href' => $scripturl . '?action=calendar',
'show' => $context['allow_calendar'],
'sub_buttons' => array(
'view' => array(
'title' => $txt['calendar_menu'],
'href' => $scripturl . '?action=calendar',
'show' => allowedTo('calendar_post'),
),
'post' => array(
'title' => $txt['calendar_post_event'],
'href' => $scripturl . '?action=calendar;sa=post',
'show' => allowedTo('calendar_post'),
),
),
),

All you need to do is copy the sub-button code into the sub_buttons array() and then edit the name, title, href and show. For example to add a sub-button to my arcade:

Code: [Select]
'arcade' => array(
'title' => 'Arcade',
'href' => 'http://www.mysite.example/arcade',
'show' => true,
'sub_buttons' => array(
'highscores' => array(
'title' => 'High Scores',
'href' => 'http://www.mysite.example/arcade/highscores.php',
'show' => true,
),
),
),



Please open a new topic if you would like more information about these instructions :D
« Last Edit: April 05, 2010, 10:30:55 PM by Ha¬≤ »
-H
Former Support Team Lead
                              I recommend:
Namecheap (domains)
Fastmail (e-mail)
Linode (VPS)