News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

How To Create Custom Pages + Tabs [2.0 RC3]

Started by IceXaos, September 28, 2010, 11:37:45 AM

Previous topic - Next topic

IceXaos

Step 1


We're going to start by creating the actions needed to get our custom page.  If you try to go to it, you'll obviously get errors as we've not yet created it.

./index.php

Find
        'movetopic2' => array('MoveTopic.php', 'MoveTopic2'),

Add After
        'mypage' => array('MyPage.php', 'MyPage'),

What this does, is when the action "mypage" is called on, it will point to ./Sources/MyPage.php and inside that, the function MyPage().

Step 2


Now, to access that page easily, we're going to create a tab to direct us there.

./Sources/Subs.php

Find
            'home' => array(
                'title' => 'Home',
                'href' => $scripturl,
                'show' => true,
                'sub_buttons' => array(
                ),
            ),


Add After
            'mypage' => array(
                'title' => 'What The Tab Says',
                'href' => $scripturl . '?action=mypage',
                'show' => true,
                'sub_buttons' => array(
                ),
            ),


This is going to add the tab we need to be able to click.  Remember to change the title.

Find
    if (isset($context['menu_buttons'][$context['current_action']]))
        $current_action = $context['current_action'];


Add After
    elseif ($context['current_action'] == 'mypage')
        $current_action = 'mypage';


Basically if you're using the action "mypage", set your current action to "mypage".  This will cause the tab to be highlighted while you're using the page.

Step 3


Now it's time to create our custom page.  Here's a basic template I use.  Remember, this doesn't exist yet!

./Sources/MyPage.php
<?php

if (!defined('SMF'))
die(
'Hacking attempt...');

function 
MyPage() {
    global 
$context;
    
$context['page_title'] = 'My Page Title Goes Here';

    function 
template_main() {
        echo 
'<center><b>Content goes here.  Remember to echo all HTML as this is in PHP.</b></center>';
    }
    
}

?>



Once you do this, you should be able to see your custom page by going to http://yoursite.com/index.php?action=mypage

If I've missed anything, let me know and I'll add it.  Also, a thanks to Cicka (I think that's how you spell it) for helping me learn a while ago, what all needs to be done.

If you have any questions, feel free to ask in a reply here.

Kays

In that custom page you do need to set a value for $context['current_action'] for the button to be active.

Or else you can change:


    elseif ($context['current_action'] == 'mypage')
        $current_action = 'mypage';


to:


    elseif (isset($_REQUEST['action']) && $_REQUEST['action'] == 'mypage')
        $current_action = 'mypage';

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

IceXaos

Quote from: Kays on September 28, 2010, 11:44:46 AM
In that custom page you do need to set a value for $context['current_action'] for the button to be active.

Or else you can change:


    elseif ($context['current_action'] == 'mypage')
        $current_action = 'mypage';


to:


    elseif (isset($_REQUEST['action']) && $_REQUEST['action'] == 'mypage')
        $current_action = 'mypage';

You don't need to, as I'm pretty sure that's set by _GET variables.  I've never really checked, but it works like that for every page I've added.

Alex' Manson

lets say my page is called "SiskoPlanet"
so this:

  'mypage' => array(
                'title' => 'What The Tab Says',
                'href' => $scripturl . '?action=mypage',
                'show' => true,
                'sub_buttons' => array(
                ),
            ),

Will Be:
'SiskoPlanet' => array(
                'title' => 'SiskoPlanet',
                'href' => $scripturl . '?action=SiskoPlanet',
                'show' => true,
                'sub_buttons' => array(
                ),
            ),

and

  elseif ($context['current_action'] == 'mypage')
        $current_action = 'mypage';

will be:

  elseif ($context['current_action'] == 'SiskoPlanet')
        $current_action = 'SiskoPlanet';

???

IceXaos

If you want the action to be called SiskoPlanet, then you'll need to change that in every location the action is stated.

zamg0d1

Ah, I will try with this one, but how to add sub-actions?

IceXaos

For a sub-action, which I assume you're talking about active tabs.

    elseif ($context['current_action'] == 'mysubpage')
        $current_action = 'mypage';


This will activate "mypage" tab if you're on "mysubpage".

Kays

Quote
You don't need to, as I'm pretty sure that's set by _GET variables.  I've never really checked, but it works like that for every page I've added.

Yes, you are correct on that. That's something new for 2.0 I wasn't aware of.

But it will only work if that page is in accessed using $_GET['action']. For other pages $context['current_action'] does need to be set.

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

IceXaos

Quote from: Kays on September 28, 2010, 12:15:55 PM
Quote
You don't need to, as I'm pretty sure that's set by _GET variables.  I've never really checked, but it works like that for every page I've added.

Yes, you are correct on that. That's something new for 2.0 I wasn't aware of.

But it will only work if that page is in accessed using $_GET['action']. For other pages $context['current_action'] does need to be set.
Yep, but adding it like this, you're kind'a forced to use actions to get to it, plus with a tab, it's just a click away.

Kays

Yes, but people do also add the menu bar to the home page of their site. Or to other pages where SSI.php is included and which are not accessed using the action array.

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

IceXaos

Quote from: Kays on September 28, 2010, 12:28:07 PM
Yes, but people do also add the menu bar to the home page of their site. Or to other pages where SSI.php is included and which are not accessed using the action array.
True.  I was just attempting to cover the main idea here.  With this, they're capable of creating pages integrated with the forum, which you access by actions, as well as using PHP along with HTML.  I couldn't possibly cover every thing a user may do.

zamg0d1

Quote from: IceXaos on September 28, 2010, 12:03:51 PM
For a sub-action, which I assume you're talking about active tabs.

    elseif ($context['current_action'] == 'mysubpage')
        $current_action = 'mypage';


This will activate "mypage" tab if you're on "mysubpage".
No more like 'forum/index.php?action=admin;area=featuresettings', so in the Admin action there's a new action, that's what I ment.

IceXaos

Quote from: zamg0d1 on September 28, 2010, 03:19:19 PM
Quote from: IceXaos on September 28, 2010, 12:03:51 PM
For a sub-action, which I assume you're talking about active tabs.

    elseif ($context['current_action'] == 'mysubpage')
        $current_action = 'mypage';


This will activate "mypage" tab if you're on "mysubpage".
No more like 'forum/index.php?action=admin;area=featuresettings', so in the Admin action there's a new action, that's what I ment.
if ($_GET['area'] == 'featuresettings') { do this }

zamg0d1

Quote from: IceXaos on September 28, 2010, 03:32:15 PM
Quote from: zamg0d1 on September 28, 2010, 03:19:19 PM
Quote from: IceXaos on September 28, 2010, 12:03:51 PM
For a sub-action, which I assume you're talking about active tabs.

    elseif ($context['current_action'] == 'mysubpage')
        $current_action = 'mypage';


This will activate "mypage" tab if you're on "mysubpage".
No more like 'forum/index.php?action=admin;area=featuresettings', so in the Admin action there's a new action, that's what I ment.
if ($_GET['area'] == 'featuresettings') { do this }
Ah you mean like this:
<?php

if (!defined('SMF'))
die(
'Hacking attempt...');

function 
Newspost() {
    global 
$context;
    
$context['page_title'] = 'My Page Title Goes Here';

    function 
template_main() {
         
$able = array('1''2''help''verzonden''none'); // pages allowed
         
$id 'none'// standard page
         
if(!empty($_GET['id']) && in_array($_GET['id'], $able)) {
            
// allowed so we load that page
            
$id $_GET['id'];
         }
         
        if (
$_GET['id'] == 'help') {         include_once("mh.gaming-freak.nl/news/news/help.php$id.php"); }
    }
    
}

?>


But with this url it won't work :( http://mh.gaming-freak.nl/forum/index.php?action=newspost;id=help



(I'm PHP noob but trying to make it work :O)

IceXaos

Why do it twice?  You're going to to check with an if statement to do certain actions anyways, so you may as well just throw it in to the main statement.

if ($_GET['id'] == '1') {
   
1();
} elseif (
$_GET['id'] == '2') {
   
2();
}

IceXaos

Sorry, just actually looked at it.  What's the point of all this?

         $able = array('1''2''help''verzonden''none'); // pages allowed
         
$id 'none'// standard page
         
if(!empty($_GET['id']) && in_array($_GET['id'], $able)) {
            
// allowed so we load that page
            
$id $_GET['id'];
         }


You check again anyways, which is gonna make it completely pointless.

Hj Ahmad Rasyid Hj Ismail

No offence IX, I prefer using a mod to do this. I am little bit dizzy with codes lately. I haven't got time to finish my latest mods too.

IceXaos

Quote from: ahrasis on September 28, 2010, 04:15:50 PM
No offence IX, I prefer using a mod to do this. I am little bit dizzy with codes lately. I haven't got time to finish my latest mods too.
Most MOD's only allow you to add a certain amount of pages, and it's not always a lot.  I believe there is one that allows up to 6, but that's as high as it goes AFAIK.  Anyways, this was made to help out someone with tab problems, but since he didn't have his pages set up within the forums, I figured I'd make a quick tutorial and show how.  He wanted to add PHP to custom pages, and I'm not sure if any MOD's allow that, but he didn't seem to have any luck with it.  I've never really tried custom page MOD's myself, since I don't see the point for something so quick.

Hj Ahmad Rasyid Hj Ismail

I agree on that. Most mods that create page don't create tab for the created page. Normally, we have to do it manually. And some of them do have limitations. There is a nice mod from arantorsmod site that can easily create tab menu. I would recommend that mod (or any mods from Arantor - well, they call him The Captain). Personally, I used cms mod (no longer available from SMF mod site. I will change to Arantor's mod later) for managing my main menu and SP for creating page.


Advertisement: