MyBB Community Forums

Full Version: Creating a custom page without using template
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello guys, need some help with custom page here. The navigation bar seems to no get displayed. Here's the code in placeholder.php


<?php
/**
 * MyBB 1.8
 * Copyright 2014 MyBB Group, All Rights Reserved
 *
 * Website: http://www.mybb.com
 * License: http://www.mybb.com/about/license
 *
 */

define("IN_MYBB", 1);
define('THIS_SCRIPT', 'placeholder.php');
require_once "./global.php";
add_breadcrumb("Placeholder", "placeholder.php");








global_header();


echo "This is a placeholder page";


global_footer();








function global_header()
{
	global $headerinclude, $context, $header, $mybb;

	echo
'
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
';
	echo'
<title>'.$mybb->settings['bbname'].' - Placeholder</title>
';
	echo $headerinclude;
	echo
'
</head>
<body>
';
	echo $header;
}

function global_footer()
{
	global $footer;

	echo $footer;
	echo
'
</body>
</html>
	';
}


?>


Hello, anyone? I am trying to make a raw custom page because if i use the other method (the one with template), I won't be able to execute PHP codes. What I'm trying to achieve is to retrieve data from mysql database and make a table in the custom page.
This is a dirty quick fix that you might be interested in:

<?php

define("IN_MYBB", 1); 
require "./global.php"; // (1)

// Edit $message to change the message!
$message = '	html here';

error($message, 'Title here');
?>
That won't do for me. Any other way to fix this? How did the navigation bar not get displayed in the first place? I thought the function add_breadcrumb should get the job done.

One other thing the function multipage doesn't work even with correct parameters supplied. Could you reproduce this?
Try

<?php
/**
 * MyBB 1.8
 * Copyright 2014 MyBB Group, All Rights Reserved
 *
 * Website: http://www.mybb.com
 * License: http://www.mybb.com/about/license
 *
 */

define("IN_MYBB", 1);
define('THIS_SCRIPT', 'placeholder.php');
require_once "global.php";

global $headerinclude, $header, $theme, $footer;

add_breadcrumb("Placeholder", "placeholder.php");

global_header();

echo "This is a placeholder page";


global_footer();

function global_header()
{
    global $headerinclude, $context, $header, $mybb;

    echo
'
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
';
    echo'
<title>'.$mybb->settings['bbname'].' - Placeholder</title>
';
    echo $headerinclude;
    echo
'
</head>
<body>
';
    echo $header;
}

function global_footer()
{
    global $footer;

    echo $footer;
    echo
'
</body>
</html>
    ';
}


?>
@Akay your answer doesn't work; you need to parse the page.

If you go to the source code, you'll find "<navigation>". Your page isn't parsed.

In inc/functions.php you'll find
/**
 * Parses the contents of a page before outputting it.
 *
 * @param string The contents of the page.
 * @return string The parsed page.
 */
function parse_page($contents)
{
	global $lang, $theme, $mybb, $htmldoctype, $archive_url, $error_handler;

	$contents = str_replace('<navigation>', build_breadcrumb(), $contents);
	$contents = str_replace('<archive_url>', $archive_url, $contents);

This is what you should use.
Instead of echoing everything, put the html in a variable. Then set that variable equal to parse_page($html). Echo this variable and it'll work.
@KevinVR, what you said totally makes sense and explain what really happen. However I can't put myself into doing what you asked. Could you point me in the right direction here? Use this more cleaned up placeholder.php:

<?php
define("IN_MYBB", 1);
define('THIS_SCRIPT', 'placeholder.php');
require_once "./global.php";

do_header();
echo "this is a placeholder page";
do_footer();

function do_header()
{
    global $headerinclude, $context, $header, $mybb;
    echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml"><head>';
    echo'<title>'.$mybb->settings['bbname'].' - Placeholder</title>';
    echo $headerinclude;
    echo '</head><body>';
    echo $header;
}

function do_footer()
{
    global $footer;
    echo $footer;
    echo '</body></html>';
}
?>

EDIT: Actually, the pagination function multipage does work. I just forgotten to echo it. Now, this navigation bar is my only problem.
This is what I came up with:

<?php
define("IN_MYBB", 1);
define('THIS_SCRIPT', 'placeholder.php');
require_once "./global.php";

do_header();
$my_page = '<navigation>';
output_page($my_page);
echo "this is a placeholder page";
do_footer();


function do_header()
{
	global $headerinclude, $context, $header, $mybb;
	echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml"><head>';
	echo'<title>'.$mybb->settings['bbname'].' - Placeholder</title>';
	echo $headerinclude;
	echo '</head><body>';
	echo $header;
}

function do_footer()
{
	echo '<br />';
	global $footer;
	echo $footer;
	echo '</body></html>';
}
?>

The result was the navigation appear inside container div - because i output it after $header. $header already comes with <div class="container">, so there's no way for me to output the $my_page var before it. Is is possible to divide $header into different separate sections, like header_welcomeback_somethig $header_searchbox?
(2015-07-29, 08:40 PM)tanuki Wrote: [ -> ]This is what I came up with:

<?php
define("IN_MYBB", 1);
define('THIS_SCRIPT', 'placeholder.php');
require_once "./global.php";

do_header();
$my_page = '<navigation>';
output_page($my_page);
echo "this is a placeholder page";
do_footer();


function do_header()
{
	global $headerinclude, $context, $header, $mybb;
	echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml"><head>';
	echo'<title>'.$mybb->settings['bbname'].' - Placeholder</title>';
	echo $headerinclude;
	echo '</head><body>';
	echo $header;
}

function do_footer()
{
	echo '<br />';
	global $footer;
	echo $footer;
	echo '</body></html>';
}
?>

The result was the navigation appear inside container div - because i output it after $header. $header already comes with <div class="container">, so there's no way for me to output the $my_page var before it. Is is possible to divide $header into different separate sections, like header_welcomeback_somethig $header_searchbox?

You don't understand what you need to do. The <navigation> is there automatically, from the header. If you use your first PHP code, open the file in chrome and show the page source. You will find <navigation> inside of it. But it should be a breadcrumbs.

Now the parse_page($entire_page_in_html); will take care of replacing <navigation> with the breadcrumbs.
What do you have to do?

Do not echo anything at all -- put every single thing inside a variable called $entire_page_in_html (for example).

Then after that is done =>
echo parse_page($entire_page_in_html);

I will post what I think is a solution (--I quickly fixed it, I didn't test it--) but you should try to make it for yourself, to learn from it.

It's possible there's still some small error inside, but have a try:
define("IN_MYBB", 1);
define('THIS_SCRIPT', 'placeholder.php');
require_once "./global.php";
add_breadcrumb("Placeholder", "placeholder.php");

$html = '';

$html .= global_header();


$html .= "This is a placeholder page";


$html .= global_footer();

echo parse_page($html);




function global_header()
{
    global $headerinclude, $context, $header, $mybb;

    return '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
' . '
<title>'.$mybb->settings['bbname'].' - Placeholder</title>
'. $headerinclude .
'
</head>
<body>
'. $header;
}

function global_footer()
{
    global $footer;

    return $footer . 
'
</body>
</html>
';
}

just tested-works.
Thank you. I see what you mean now. This is the final code:

<?php
define("IN_MYBB", 1);
define('THIS_SCRIPT', 'placeholder.php');
require_once "./global.php";
add_breadcrumb("Placeholder", "placeholder.php");



do_header();
echo 'This is a placeholder page';
do_footer();



function do_header()
{
	global $headerinclude, $context, $header, $mybb;
	echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<title>'.$mybb->settings['bbname'].' - Placeholder</title>
	'.$headerinclude.'
	</head>
	<body>';
	global $header;
	echo parse_page($header);
}

function do_footer()
{
	global $footer;
	echo $footer;
	echo '</body>
	</html>';
}
?>

Thank you, KevinVR and the rest.