MyBB Community Forums

Full Version: Integrating into template with variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I am trying to have my plugin integrate a global template into all index page templates. So far, I am able to make the template and modify all the index templates. However, there doesn't seem to be a change on the pages.


Here is my code

<?php

if(!defined("IN_MYBB"))
{
	die("You cannot access this file directly. Please Make Sure IN_MYBB Is Defined");
}

function serverStatus_info()
{
	return array(
		"name" => "",
		"description" => "",
		"website" => "www.com",
		"author" => "test",
		"version" => ".1",
		"guid" => "",
		"compatibility" => "*"
		);
}

function serverStatus_activate()
{
	global $db, $templates, $plugins;
	require MYBB_ROOT.'/inc/adminfunctions_templates.php';
    $plugins->add_hook("index_start", "serverStatus_index");


  	//Get and set the template
  	$template = '<h5>This is where the server information is going to go. Prepare to have your mind blown</h5>';

  	//file_get_contents('serverInfo.tmplt');
    find_replace_templatesets( "index", '#'.preg_quote('{$announcement}').'#', '{$announcement}{$serverStatusInfo}' ); 
  	$db->insert_query('templates', array('title' => 'serverInfo', 'sid' => '-1', 'template' => $db->escape_string($template), 'version' => '1411', 'status' => '', 'dateline' => TIME_NOW));

}

function serverStatus_deactivate()
{
	global $db;
    require MYBB_ROOT.'/inc/adminfunctions_templates.php';


    find_replace_templatesets( "index", '#'.preg_quote('{$announcement}').'#','{$announcement}', 0 ); 

}

function serverStatus_index()
{
    global $templates;
    eval("\$serverStatusInfo = \"".$templates->get('serverInfo')."\";");
}

?>

This is the template code that I put in

{$serverStatusInfo}

and this is the simple test template

<h5>Test Template</h5>

Any help is much appreciated.
plugin hook needs to be moved out of the function
and serverStatus_index function needs to have $serverStatusInfo as global variable
Yes, the plugin hook should outside any function and in your _deactivate you weren't properly removing your template edits. Try this: (or something like it)

function serverStatus_activate()
{
    global $templates;
    require MYBB_ROOT.'/inc/adminfunctions_templates.php';

	//Get and set the template
	$template = '<h5>This is where the server information is going to go. Prepare to have your mind blown</h5>';

    find_replace_templatesets('index', '#' . preg_quote('{$announcement}') . '#', '{$announcement}{$serverStatusInfo}');
	
	/*
	 * this should happen in _install() not here
	 $db->insert_query('templates',
array('title' => 'serverInfo', 'sid' => '-1', 'template' => $db->escape_string($template), 'version' => '1411', 'status' => '', 'dateline' => TIME_NOW));
	 */
}

function serverStatus_deactivate()
{
    require MYBB_ROOT.'/inc/adminfunctions_templates.php';
    find_replace_templatesets( "index", '#' . preg_quote('{$announcement}') . '#', '');
}

function serverStatus_index()
{
    global $templates, $serverStatusInfo;
    eval("\$serverStatusInfo = \"" . $templates->get('serverInfo') . "\";");
}
That was it. Thank you!