Uutiset:

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

Main Menu
Advertisement:

Insert JavaScript into index.template.php with integration hooks

Aloittaja davidhs, marraskuu 14, 2013, 07:17:07 AP

« edellinen - seuraava »

davidhs

I am writing a mod for SMF 2.0.x and I want insert a JavaScript file into head.

I see http://www.simplemachines.org/community/index.php?topic=425070.0 but I do not want to modify the source code.

If I use integrate_pre_include/integrate_pre_load it does not work (still there is no variable $context) and I have seen it work with integrate_theme_include/integrate_load_theme
LainaaMyJs.php

function add_js()
{
global $context, $settings;

$context['html_headers'] .= '
<script type="text/javascript" src="' . $settings['default_theme_url'] . '/scripts/MyJs.js?fin20"></script>';
}


I add this to my MOD

$hooks = array(
'integrate_theme_include' => array('$sourcedir/MyJs.php'),
'integrate_load_theme' => array('add_js'),
...
);

if (!empty($context['uninstalling']))
$call = 'remove_integration_function';
else
$call = 'add_integration_function';

foreach ($hooks as $hook => $function)
$call($hook, $function);

Is it correct tp use here integrate_theme_include/integrate_load_theme? Or should I use another hook function?

emanuele

Yep, the combo integrate_theme_include/integrate_load_theme is find for that kind of things.

As a suggestion, you should use some more "specific" function name. Generic names like add_js are likely to create conflict among mods (for example if two mods were to use a function named add_js it would result in a php error ;)).
I usually use a prefix to name all the functions that belong to a mod.


Take a peek at what I'm doing! ;D




Hai bisogno di supporto in Italiano?

Aiutateci ad aiutarvi: spiegate bene il vostro problema: no, "non funziona" non è una spiegazione!!
1) Cosa fai,
2) cosa ti aspetti,
3) cosa ottieni.

davidhs

Thanks!

Lainaus käyttäjältä: emanuele - marraskuu 14, 2013, 09:09:57 AP
As a suggestion, you should use some more "specific" function name. Generic names like add_js are likely to create conflict among mods (for example if two mods were to use a function named add_js it would result in a php error ;)).
I usually use a prefix to name all the functions that belong to a mod.
I also do this (use a sufix in all functions, settings vars and files of a mod), but was only an example ;)

emanuele



Take a peek at what I'm doing! ;D




Hai bisogno di supporto in Italiano?

Aiutateci ad aiutarvi: spiegate bene il vostro problema: no, "non funziona" non è una spiegazione!!
1) Cosa fai,
2) cosa ti aspetti,
3) cosa ottieni.

Advertisement: