View Full Version : append after <head> in all templates?
Dr.CustUmz
04-13-2016, 06:25 PM
This turned out to be a global replace that is very handy if you know how to use it, you can find the solution begining in post 18(for vb4) and 20 (for vb3)
So I know if I was going to add something after <head> to one template I could do it something like:$find = '<head>';
$add = '<something>';
$vbulletin->templatecache['FORUMHOME'] = str_replace($find,$find.$add,$vbulletin->templatecache['FORUMHOME']);
but how would I go about doing this to every page, since <head> is contained in templates, and not in the headinclude it makes what I'm trying to do a little more complicated.
Tried this and a few other combinations with no luck:
$find = '<head>';
$add = '<something>';
$find = str_replace($find,$find.$add,$find);
squidsk
04-14-2016, 12:11 AM
Why not just add it with a template hook. There are a couple in head or its sub templates.
Dr.CustUmz
04-14-2016, 11:40 AM
because im trying to change <head> to lol, just learning experiance
What exactly are you trying to add to <head>? Maybe there's another way to do it instead of replacing it.
MarkFL
04-14-2016, 11:44 AM
If I recall correctly, what I have done in vB 3.8.9 to add something to pages is append it to either $spacer_open or $spacer_close. :)
Dr.CustUmz
04-14-2016, 11:53 AM
If I recall correctly, what I have done in vB 3.8.9 to add something to pages is append it to either $spacer_open or $spacer_close. :)
$spacer_open consists of I believe 3 divs and first appears in the header template which is not the <head> tag,
this is just an example not what I'm doing, but say I wanted to replace
<head>
with
<head class="someClass">
<head> is not located in the headinclude template which IMO is where it should be, but instead pretty much every main template has its own <head> tag.
so its looking like if I want to do something like this I'll have to create a replace for every template
$find = '<head>';
$replace = '<head class="someClass">';
$vbulletin->templatecache['FORUMHOME'] = str_replace($find,$replace,$vbulletin->templatecache['FORUMHOME']);
$vbulletin->templatecache['showthread'] = str_replace($find,$replace,$vbulletin->templatecache['showthread']);
etc...
MarkFL
04-14-2016, 12:01 PM
What do you get by adding CSS to the head element?
Dr.CustUmz
04-14-2016, 12:04 PM
that is not what im doing was just an example lol
If it really turns out to be difficult to do this, you can write some native JavaScript to do this.
http://stackoverflow.com/questions/2676336/append-some-html-into-the-head-tag
Dr.CustUmz
04-14-2016, 12:23 PM
the end result will actually be for spiders, and i dont believe spiders see execute js
--------------- Added 1460644385 at 1460644385 ---------------
why does this method not work?
<head> = str_replace('head', 'head class="test"', <head>);
MarkFL
04-14-2016, 12:44 PM
...why does this method not work?
<head> = str_replace('head', 'head class="test"', <head>);
Because "<head>" is not a string variable. :)
Dr.CustUmz
04-14-2016, 01:04 PM
hmmm, im no good with preg replaces, but shouldn't something like this work, on either global or parse templates.
this is not working
$find = preg_replace("/<head[^>]>/i", "TEST", $find);
MarkFL
04-14-2016, 01:23 PM
hmmm, im no good with preg replaces, but shouldn't something like this work, on either global or parse templates.
this is not working
$find = preg_replace("/<head[^>]>/i", "TEST", $find);
You don't need to use regex here with preg_replace(), but what you need is to have the template code stored as a string, and then just use str_replace() on the string.
squidsk
04-14-2016, 01:40 PM
What exactly are you trying to do because the head tag does not take any attributes, so there's literally nothing you can change with the actual tag itself. The contents can be changed but as I said that's easily accomplished with template hooks.
Dr.CustUmz
04-14-2016, 02:09 PM
im trying to do something along the lines of (again not the code I'm actually using)
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://ogp.me/ns#" xmlns:fb="http://www.facebook.com/2008/fbml">
I was just using the head tag as filler, everything I'm trying to accomplish is the same concept and would be achieved the same way regardless.
so in the end i can replace a tag across multiple templates.
so think of it this way since instead of head, everything on here says body.
i want to add an onload event to all body tags, in every template, without having to say every template.
so we do something like this for every template there is a body tag:
$find = '<body>';
$replace = '<body onload="myFunction()">';
$vbulletin->templatecache['FORUMHOME'] = str_replace($find,$replace,$vbulletin->templatecache['FORUMHOME']);
$vbulletin->templatecache['showthread'] = str_replace($find,$replace,$vbulletin->templatecache['showthread']);
etc...
i'm simply asking is there a way to do this globally so you dont have to specify every template.
this would be an example of it, but this doesnt work:
$find = '<body>';
$replace = '<body onload="myFunction()">';
$find = str_replace($find, $replace, $find);
MarkFL
04-14-2016, 02:48 PM
What I would do to add an onload event to the body element on all pages is in a plugin hooked to "parse_templates" with the following:
$spacer_close .= '<script>
function myFunction()
{
alert("Hello!");
}
var el = document.getElementsByTagName("html")[0];
el.innerHTML = el.innerHTML.replace("<body>", "<body onload=\"myFunction();\">");
</script>';
However, there are better ways to add an event to a document, I am just trying to follow the example you gave. :)
Dr.CustUmz
04-14-2016, 03:06 PM
mark thanks for always being around to help, but I feel like I'm wasting your time =/
I was only throwing another example to what could be achieved with the end result I'm looking for.
so lets look at what I'm intending on doing:
In every template that has a <html> tag, i want to add some attributes.
so lets say for just a one attribute example this is exactly what i want to do.
across every single template:
$find = '<html>';
$replace = '<html lang="en">';
$find = str_replace($find, $replace, $find);
we've already established that wont work,
but the end result should be something like this if it's achievable.
instead of doing:
$find = '<html>';
$replace = '<html lang="en">';
$vbulletin->templatecache['Template1'] = str_replace($find,$replace,$vbulletin->templatecache['Template1']);
$vbulletin->templatecache['Template2'] = str_replace($find,$replace,$vbulletin->templatecache['Template2']);
$vbulletin->templatecache['Template3'] = str_replace($find,$replace,$vbulletin->templatecache['Template3']);
$vbulletin->templatecache['Template4'] = str_replace($find,$replace,$vbulletin->templatecache['Template4']);
$vbulletin->templatecache['Template5'] = str_replace($find,$replace,$vbulletin->templatecache['Template5']);
$vbulletin->templatecache['Template6'] = str_replace($find,$replace,$vbulletin->templatecache['Template6']);
$vbulletin->templatecache['Template7'] = str_replace($find,$replace,$vbulletin->templatecache['Template7']);
etc....
MarkFL
04-14-2016, 06:06 PM
This works on my vB 4.2.x dev site...a plugin hooked at "parse_templates":
$t_arr = $vbulletin->templatecache;
$t_keys = array_keys($t_arr);
$count = count($t_keys);
for ($n = 0; $n < $count; $n++)
{
if (strpos($t_arr[$t_keys[$n]], '<body') !== false)
{
$vbulletin->templatecache[$t_keys[$n]] = str_replace('<body', '<body name="myName"', $vbulletin->templatecache[$t_keys[$n]]);
}
}
Dr.CustUmz
04-14-2016, 07:00 PM
causes everything to go white with no page source lol
MarkFL
04-14-2016, 09:22 PM
causes everything to go white with no page source lol
Yeah, I got the same on my vB 3.8.9 dev site, but I thought it worth a shot since it works with vB 4.2.x. :)
--------------- Added 1460691040 at 1460691040 ---------------
This works on my vB 3.8.9:
$t_arr = $vbulletin->templatecache;
$t_keys = array_keys($t_arr);
$count = count($t_keys);
for ($n = 0; $n < $count; $n++)
{
if (strpos($t_arr[$t_keys[$n]], '<body') !== false)
{
$vbulletin->templatecache[$t_keys[$n]] = str_replace("<body", "<body name='myName'", $vbulletin->templatecache[$t_keys[$n]]);
}
}
Dr.CustUmz
04-15-2016, 01:41 AM
tried your posted first and was like hey this is exactly what i was looking for =)
put in
$t_arr = $vbulletin->templatecache;
$t_keys = array_keys($t_arr);
$count = count($t_keys);
for ($n = 0; $n < $count; $n++)
{
if (strpos($t_arr[$t_keys[$n]], '<html') !== false)
{
$vbulletin->templatecache[$t_keys[$n]] = str_replace("<html", "<html tag='test'", $vbulletin->templatecache[$t_keys[$n]]);
}
}
that worked too!
so this little snippet is more useful than one might think, this is a global replacer and its wonderful =) thanks mark!
MarkFL
04-15-2016, 01:46 AM
Glad to help, Ryan!
One thing I still don't understand is why, when I tried using a foreach loop to iterate through the $vbulletin->templatecache array, it would only get the last template in the array (same issue with vB 4). But, using the for loop worked, so I went with that. :D
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.