News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Using hooks from a theme?

Started by Bloc, August 11, 2017, 03:19:05 AM

Previous topic - Next topic

Bloc

I am working on a new theme for SMF 2.1 beta3/4 and contemplating whether I should make a specific mod to add hooks - or whether hooks can be added from inside the theme itself, in a correct fashion?

My concerns are 1) once a hook is added through the theme(meaning only the theme holds the function/file) , will other themes error out when trying to access said function? and 2) is there a way to check whther a hook has been added(through the theme) so it won't do the adding it each and every time the theme is used?

Using a separate mod solves both these things..but I wonder if it can be done safely just from the theme too.

Anyone has some insights?


@live627 posted an example some time ago:
Quote from: live627 on August 19, 2016, 07:03:03 PM
Complete template override? Easy. I have some code I wrote several years ago to do this very thing:

function template_init()
{
add_integration_function('integrate_menu_buttons', 'fixSubTemplates', false);
}

function fixSubTemplates()
{
global $context;

$new_sub_template = empty($context['sub_template']) ? 'main_override' : $context['sub_template'] . '_override';

if (is_callable('template_' . $new_sub_template))
$context['sub_template'] = $new_sub_template;
}


The hook should be late enough in the runtime where you know the templates will have been already loaded, otherwise the overrides won't work.

Arantor

It kinda depends on what you're trying to do as to whether hooks will be called at the right time or not.

Bloc

Intially I am looking to add to the posting screen: "integrate_post_end" and "integrate_post2_end" to do some swapping between input fields and the body of the text(unless I can find a way to do that just by javascript).

But the most important goal is to integrate into "integrate_pre_parsebbc" and "integrate_bbc_codes" to add a single BBC tag that will be invisible in normal posts, but can be targeted by the theme. My idea is to store theme specifc values about the post in this bbc tag, but without showing it on posts(hidden by css classes). The theme will use this to display extra info, and/or display things differently. The advantage is that once you switch to another theme the info isn't gone - but has this extra BBC tag around it so you know what is what.

Bloc

Looking at the "call_integration_hook" function it seems I have a few options..one is to NOT make it permanent - that would tie in neatly with only running it from the theme and it also seems the hook isn't run if the function doesn't exist.  Should perhaps solve both my intial concerns then. :)


Advertisement: