MyBB Community Forums

Full Version: How to Re-Edit Templates When A Theme Is Imported
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have seen this too many times to count and the fix is quite simple.

Mods will install template or template mods but will not auto update the templates when you import a new theme.

Here is a quick/guide example using the "Thanks" MyBB plugin.

In the top add:

$plugins->add_hook('admin_style_themes_import_commit', 'thx_theme_added');

somewhere else in the file add:

function thx_theme_added()
{
	global $db;
	require_once MYBB_ROOT."inc/adminfunctions_templates.php";
	find_replace_templatesets("postbit", '#'.preg_quote('{$post[\'thxdsp_inline\']}').'#', '', 0);
	find_replace_templatesets("postbit", '#'.preg_quote('{$post[\'thxdsp_outline\']}').'#', '', 0);
	find_replace_templatesets("postbit", '#'.preg_quote('{$post[\'thanks\']}').'#', '', 0);
	find_replace_templatesets("postbit_classic", '#'.preg_quote('{$post[\'thxdsp_inline\']}').'#', '', 0);
	find_replace_templatesets("postbit_classic", '#'.preg_quote('{$post[\'thxdsp_outline\']}').'#', '', 0);
	find_replace_templatesets("postbit_classic", '#'.preg_quote('{$post[\'thanks\']}').'#', '', 0);
	find_replace_templatesets("headerinclude", "#".preg_quote('<script type="text/javascript" src="jscripts/thx.js"></script>').'#', '', 0);

	$db->delete_query("templates", "title='thanks_postbit_count'");
	$db->delete_query("templates", "title='thanks_postbit_inline'");
	$db->delete_query("templates", "title='thanks_postbit_inline_classic'");
	$db->delete_query("templates", "title='thanks_postbit_outline'");
	if(!find_replace_templatesets("postbit", '#'.preg_quote('{$seperator}').'#', '{$post[\'thxdsp_inline\']}{$seperator}{$post[\'thxdsp_outline\']}'))
	{
		find_replace_templatesets("postbit", '#button_delete_pm(.*)<\/tr>(.*)<\/table>#is', 'button_delete_pm$1</tr>{\$post[\'thxdsp_inline\']}$2</table>{$post[\'thxdsp_outline\']}');
	}
	find_replace_templatesets("postbit", '#'.preg_quote('{$post[\'button_quote\']}').'#', '{$post[\'button_quote\']}{$post[\'thanks\']}');
	find_replace_templatesets("postbit_classic", '#button_delete_pm(.*)<\/tr>(.*)<\/table>#is', 'button_delete_pm$1</tr>{\$post[\'thxdsp_inline\']}$2</table>{$post[\'thxdsp_outline\']}');
	find_replace_templatesets("postbit_classic", '#'.preg_quote('{$post[\'button_quote\']}').'#', '{$post[\'button_quote\']}{$post[\'thanks\']}');
		
	find_replace_templatesets("headerinclude", "#".preg_quote('{$newpmmsg}').'#',
		'<script type="text/javascript" src="jscripts/thx.js"></script>{$newpmmsg}');
	
	$templatearray = array(
		'title' => 'thanks_postbit_count',
		'template' => "<div><span class=\"smalltext\">{\$lang->thx_thank} {\$post[\'thank_count\']}<br />
	{\$post[\'thanked_count\']}<br /></span></div>",
		'sid' => '-1',
		);
	$db->insert_query("templates", $templatearray);

	$templatearray = array(
		'title' => 'thanks_postbit_inline',
		'template' => "<tr id=\"thx{\$post[\'pid\']}\" style=\"{\$display_style}\" class=\"trow2 tnx_style tnx_newstl\"><td><span class=\"smalltext\">{\$lang->thx_givenby}</span>&nbsp;<span id=\"thx_list{\$post[\'pid\']}\">\$entries</span></td></tr>",
		'sid' => '-1',
		);	
	$db->insert_query("templates", $templatearray);
	
	$templatearray = array(
		'title' => 'thanks_postbit_inline_classic',
		'template' => "<tr id=\"thx{\$post[\'pid\']}\" style=\"{\$display_style}\" class=\"trow2 tnx_style tnx_classic\"><td><span class=\"smalltext\">{\$lang->thx_givenby}</span></td><td class=\"trow2 tnx_style\" id=\"thx_list{\$post[\'pid\']}\">\$entries</td></tr>",
		'sid' => '-1',
		);	
	$db->insert_query("templates", $templatearray);

	$templatearray = array(
		'title' => 'thanks_postbit_outline',
		'template' => "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\" id=\"thx{\$post[\'pid\']}\" style=\"{\$display_style};margin-top:5px;\"><tr><td>
		<table border=\"0\" cellspacing=\"{\$theme[\'borderwidth\']}\" cellpadding=\"{\$theme[\'tablespace\']}\" class=\"tborder thxdsp_outline\"><tr class=\"trow1 tnx_style\"><td valign=\"top\" width=\"1%\" nowrap=\"nowrap\"><img src=\"{\$mybb->settings[\'bburl\']}/images/rose.gif\" align=\"absmiddle\" /> &nbsp;<span class=\"smalltext\">{\$lang->thx_givenby}</span></td><td class=\"trow2 tnx_style\" id=\"thx_list{\$post[\'pid\']}\">\$entries</td></tr></table>
		</td></tr></table>",
		'sid' => '-1',
		);
	$db->insert_query("templates", $templatearray);	
}

What this does is it hooks into the theme importing and it removes all template edits to prevent duplicate mods from appearing depending on the regex, and it re-installs them.

This will happen ANY time you import a theme. Now you COULD just use the activate/deactivate, but you cant have any settings being inserted or every theme import would wipe your settings and would be a bad design..


For some this is common sense, though i hope this will make it easier to add the ability to re-install your plugin when a theme is added.

Peace..
Interesting, but curious about why you delete all plugin templates?
So it doesn't have the edit twice in ones that have already been edited.
That is the reason to why he removed the edits before adding them all back. But he also removed all plugin templates and added them back, but those are in the global templates set, aren't they (that is actually my question)?
Didn't notice that, I don't know then.