News:

Wondering if this will always be free?  See why free is better.

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.




Computer Robot

How can I modify this to point to a parent directory.  I just want a button to go to the home page of my website but the forum folder is in a subdirectory of the homepage.  ie http://www.homepcrepairsandupgrades.co.uk/forum/

I added the first 2 steps from the original post to make a "Home Page" tab after Home but its not directed to anything. 

Computer Robot

The new tab is actually directed to the forum home page.  I think this is because I change the "mypage" into "home page" somewhere in the code and its calling that function.  What I want though is to go to http://www.homepcrepairsandupgrades.co.uk/  a parent directory of my forum. 

IceXaos

Quote from: Computer Robot on December 08, 2010, 12:30:18 PM
The new tab is actually directed to the forum home page.  I think this is because I change the "mypage" into "home page" somewhere in the code and its calling that function.  What I want though is to go to http://www.homepcrepairsandupgrades.co.uk/  a parent directory of my forum. 
Is the "homepage" your linking to off-site, or is it the actual homepage of the forum?  If it's off-site, you will not need to do the 2nd part of step 2.  Now, you can name the action whatever you like as long as it's not taken, simply set the title to what you'd like it to say and the href to the link you just said.  If you end up lost, let me know and I can help you over teamviewer and show you how, or I can modify it for you and send it back.

Computer Robot

HI thanks it worked.  I tried to change the 'Home' button to 'Forum Home'  so I could give my homepage that title but it shrank the button to minimal width and no text.  I then removed the $txt from the code and it went back to normal.  Shouldn't be a problem should it?

Hj Ahmad Rasyid Hj Ismail

IMO, if you follow the OP correctly, then you will realized that there is no need to put $txt as that is used only if you want to call the menu from language file.

Computer Robot

Quote from: ahrasis on December 09, 2010, 05:07:52 AM
IMO, if you follow the OP correctly, then you will realized that there is no need to put $txt as that is used only if you want to call the menu from language file.

Sorry dont know what that means being a noob.  I will point out though that I changed one of the original buttons installed along with the forum i.e the HOME tab. 

IceXaos

When you use, for example $txt['home_tab'] you are calling on the language file where that variable is defined.  There is no reason to define them in the language file unless you're anal, in which you could also create a template file rather than stick everything inside of the source file.

Rather than use something like $txt['home'] just use 'home'.

radioz

IceXaos,

Could your step #2 be used for an image?  I have managed to change the smflogo and move it to the left.I would like to link that image to the home page of my external main website.  I am using a veryy slightly modified (logo) Curve theme,

Thanks!

Jon

IceXaos

Quote from: radioz on December 10, 2010, 07:37:50 AM
IceXaos,

Could your step #2 be used for an image?  I have managed to change the smflogo and move it to the left.I would like to link that image to the home page of my external main website.  I am using a veryy slightly modified (logo) Curve theme,

Thanks!

Jon
If you're wanting a regular image linked to another page, you would simply use HTML code.
<a href="http://...."><img src="http://.../*.jpg"></a>

Or, did you mean you wanted to add an image to the drop-down menu?  If so, did you mean add an icon, or a small banner?

advance

I've got a different question. I want to make a button ( and sub buttons ) that direct to the pages on another server but those pages need to be rendered inside forum, not in new page/window.

Sorry for my english :).

Regards.

IceXaos

Quote from: advance on December 13, 2010, 07:16:45 AM
I've got a different question. I want to make a button ( and sub buttons ) that direct to the pages on another server but those pages need to be rendered inside forum, not in new page/window.

Sorry for my english :).

Regards.
There are two ways you can go about doing this, but if you have many questions, please make a thread in the coding support for more information.

You could go about it using Javascript/JQuery using a new page like this and only inserting a div into the page, which can contain data by default and will be changed as you choose different links (which would link to use JQuery, not a URL).

Alternatively, you could use an iFrame on the page, and this should be pretty self-explanatory.

Good luck.

Hack-King


IceXaos


lucas-ruroken

Adk Portal 3.1 is coming....

Design your universe!

IceXaos

Quote from: lucas-ruroken on January 03, 2011, 01:05:17 AM
so.. good tip
:) Figured it may be of use as it's something that took me a little to figure out when I first started using SMF.  For me, the problem was finding everything, but this should cover people who don't really know PHP, and those who need help locating things.

Almost 3K views .. :P

lucas-ruroken

Adk Portal 3.1 is coming....

Design your universe!

Hj Ahmad Rasyid Hj Ismail

Quote from: IceXaos on January 03, 2011, 12:48:58 PM
Quote from: lucas-ruroken on January 03, 2011, 01:05:17 AM
so.. good tip
:) Figured it may be of use as it's something that took me a little to figure out when I first started using SMF.  For me, the problem was finding everything, but this should cover people who don't really know PHP, and those who need help locating things.

Almost 3K views .. :P
IX - This thread is indeed useful. Plus your avatar and sig pic might be distracting them too  8)

inter

Excuse for my English. :P

Thanks for this instruction, but are necessary for me more information how to make that a page title was added in a menu tree?

How to make on this page subpage in which scenarios were fulfilled:

http://myforum/index.php?action=mypage;sa=?;in=?;?=?;...

Sorry for my English

lucas-ruroken

in your custom function.... You can add

global $context;

$context['linktree'][] = array(
'url' => 'YOUR URL',
'name' => 'YOUR TITLE'
);
Adk Portal 3.1 is coming....

Design your universe!

inter

Quote from: lucas-ruroken on January 11, 2011, 10:37:45 AM
in your custom function.... You can add

global $context;

$context['linktree'][] = array(
'url' => 'YOUR URL',
'name' => 'YOUR TITLE'
);

I don't understand  :P
Sorry for my English

Mikirin

I'm sorry for not reading on, but I am wondering, does it work for external links too?

lucas-ruroken

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

    $context['linktree'][] = array(
        'url' => 'YOUR URL',
        'name' => 'NAME OF THE LINKTREE',
    );


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

Design your universe!

inter

Quote from: lucas-ruroken on January 11, 2011, 12:28:21 PM
Quote
function MyPage() {
    global $context;
    $context['page_title'] = 'My Page Title Goes Here';

    $context['linktree'][] = array(
        'url' => 'YOUR URL',
        'name' => 'NAME OF THE LINKTREE',
    );


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

:D super!!!

And how to do similar for subpage???

http://localhost/forum/index.php?action=mypage;sa=post; ...

sorry my English  :P
Sorry for my English

lucas-ruroken

It's more complex than that....

You can open one mod That use it... And check it.
Adk Portal 3.1 is coming....

Design your universe!


inter

#47
How to connect language files?

How to connect .Themes/default/MyPage.template.php?




global $context, $txt, $modSettings;

I have a little understood :)
Sorry for my English

lucas-ruroken

in your function... add

if(loadLanguage('MyPage') == false)
   loadLanguage('MyPage','english');


if not load MyPage.yourlanguage.php, load MyPage.english.php ;)
Adk Portal 3.1 is coming....

Design your universe!

inter



$subActions = array(

'view' => 'ViewPage',
);


'view' - http://localhost/forum/index.php&action=mypage;sa=view

=> 'ViewPage', - function for display subpage ???

Sorry for my English

lucas-ruroken

exactly...

you can use that as example.....


<?php

function MyPage()
{
//Load our custom template MyPage.template.php
loadTemplate('MyPage');

//Load our subactions sa=
$sa = array(
//This is the default action, you can load it action=mypage or action=mypage;sa=default... it's the same
'default' => 'DefaultFunction',
'download' => 'DownloadFunction',
'another' => 'AnotherFunction',
);

//that is for load subactions... if you are requesting a subaction, load it.... If It doesn't.... load defaulfunction
if(!empty($_REQUEST['sa']) && !empty($sa[$_REQUEST['sa']]))
$sa[$_REQUEST['sa']]();
else
DefaultFunction();

}

function 
DefaultFunction()
{
global $context$txt;

//Set the page title
$context['page_title'] = 'Default Page';

//Sub template.... You need to add in your MyPage.template.php function template_lucas_default()
$context['sub_template'] = 'lucas_default';

}

function 
DownloadFunction()
{
global $context$txt;

//Set the page title
$context['page_title'] = 'Download Page';

//Sub template.... You need to add in your MyPage.template.php function template_lucas_download()
$context['sub_template'] = 'lucas_download';

}

function 
AnotherFunction()
{
global $context$txt;

//Set the page title
$context['page_title'] = 'Another Page';

//Sub template.... You need to add in your MyPage.template.php function template_lucas_another()
$context['sub_template'] = 'lucas_another';

}


?>


It's very simple.... after that, you need add the differentes function template_ in your template file
Adk Portal 3.1 is coming....

Design your universe!

larryhyman

#51
I have been using this as a template: (please correct me if I am wrong)


<?php
$_REQUEST
['action'] = 'myshop';
$_GET['action'] = 'myshop';
require(
'smforums/SSI.php');
$context['page_title_html_safe'] = 'Welcome to My Shopping Cart';
$context['linktree'][] = array(
'url' => './myshop.php',
'name' => 'Shop',
);
template_header();
?>

<div class="windowbg" style="width:100%; margin-left: auto;margin-right: auto;">
   <span class="topslice"><span></span></span>
   <div class="content">
<!-- start html content -->



<!-- end html content -->
</div>
   <span class="botslice"><span></span></span>
</div>
<?php
template_footer
();
?>


This has been working very fine for me....
In god we trust, all others pay cash !

http://hyman.sytes.net

larryhyman

I could not find the original post I got this info from but I found this....

Quote from: [SiNaN] on August 23, 2008, 01:47:26 PM
Yeah, I know. You must be using $ssi_layers = array('main');. As I understand, you want to set the current action in a custom page. To do this, before including SSI.php, add this code:

$_GET['action'] = 'custom';

When you are on that custom page, the $context['current_action'] will be equal to 'custom'.


and this...

Quote from: Arantor on April 24, 2010, 09:43:07 AM
$current_action is set based on the value of $_REQUEST['action'] which you'll see earlier in the code.

I believe you can set $_REQUEST['action'] in your code (BEFORE SSI.php is loaded) to 'mypage' or something, and then define 'mypage' => array(......) in the menu.

Everything does work fine for me....


In god we trust, all others pay cash !

http://hyman.sytes.net

larryhyman

OK, I found the original post, I would really like to get some feedback on this, as it works for me, but is it wrong?

Quote from: ledor on September 21, 2010, 09:48:19 PM
I haven't seen this one. I'll read this and let you know later. Thanks.


Edit:
At last, I've solved my problem. Thanks for that link although it doesn't directly solved my problem but was able to find a solution based on the discussion. Here's what I've done:
Instead of adding

$context['current_action'] = 'home'

to my index.php (homepage), I've added the

$_REQUEST['action'] = 'home';
$_GET['action'] = 'home';


Then, on Subs.php I've changed the default current action to forum:

// Default to home. >> changed from 'home' to 'forum'
$current_action = 'forum';


That solved my problem.

Thanks a lot KEA!!!

In god we trust, all others pay cash !

http://hyman.sytes.net

Advertisement: