Quantcast
Channel: MyBB Community Forums - Tutorials
Viewing all articles
Browse latest Browse all 685

Use MyBB Header/Footer externally

$
0
0
I don't know why this took me so long to figure out, but I figured I'd post this in case anyone else is wondering how to do this. Basically, what this does is it takes the header and footer of your forum and puts it on a page outside of your forum. Those of you that are familiar with SMF's SSI.php will thank me for this Smile

Let's say you want your page to be the root index.php page. Assuming your forum is in /forum/
First, make a page called header.php and put this code

If you do this, make sure you change your cookie path to /
Admin Panel > Configuration > Settings > Site Details > Cookie Path

[Image: WRU8vWl.png]

PHP Code:
<?php
// define forum directory folder; this is assuming your forum URL is example.com/forum/index.php
chdir('forum'); 

// basically a key, don't change this
define('IN_MYBB'1); 

// require global.php from your forum
require './global.php'

// require init.php from your forum; if you don't have this, you won't be able to stay logged in
require_once './inc/init.php'

// include your css/js stuff from <head></head>
 
   echo $headerinclude

// include your header
 
   echo $header
?>

Next, make a page called footer.php and put this code
PHP Code:
<?php
// output the footer from the forum
echo $footer;
?>

Now, on your index.php page, you'll need to include both of those files like this

PHP Code:
<?php
include 'header.php';
include 
'footer.php';
?>


Now, whatever you want to put on that page, put it like this
PHP Code:
<?php
include 'header.php';

echo 
'content goes here';

include 
'footer.php';
?>

OR if you're not really familiar with PHP


PHP Code:
<?php
include 'header.php';
?>

You can use HTML here.

<?php
include 'footer.php';
?>



Let's say you want to get some data from the user
This will grab the username, usergroup, and email from the user that is viewing the page if they're logged in.

PHP Code:
<?php
include 'header.php';

if(
$mybb->user['uid']) { // check if user is logged in

    $username $mybb->user['username']; // set username variable[/font]
    $usergroup $mybb->user['usergroup']; // set usergroup variable
    $email $mybb->user['email']; // set email variable
    
/*you don't need to set the variables above, it's completely optional, but it reduces redundancy in the long run. So rather than having to type out $mybb->user['username'] over and over, you can just type in $username */

    echo '<p>'.$username.'</p>'// outputs $username
    echo '<p>'.$usergroup.'</p>'// outputs $usergroup
    echo '<p>'.$email.'</p>'// outputs $email
}

include 
'footer.php'
?>


Now, let's say you wanted to restrict a page so only admins can see the page (assuming admin usergroup is 4)


PHP Code:
<?php
include 'header.php';

if(
$mybb->user['uid'] && $mybb->user['usergroup'] == 4) { //user is an admin, execute code
echo 'super top secret hidden stuff';
}
else { 
// user is not an admin, so let them know
echo 'You are not an admin!';
}

include 
'footer.php';
?>


If you have any questions or need any help, I'll gladly help you out. I'm not very good with PHP though.

Viewing all articles
Browse latest Browse all 685

Trending Articles