PDA

View Full Version : How to add php code into vb4 template?


postcd
08-03-2014, 12:17 PM
How to add php code into vbulletin 4 template?
i want it only on the index page (forums listing)

the php code to add:

<?php
// THE FOLLOWING BLOCK IS USED TO RETRIEVE AND DISPLAY LINK INFORMATION.
// PLACE THIS ENTIRE BLOCK IN THE AREA YOU WANT THE DATA TO BE DISPLAYED.

// MODIFY THE VARIABLES BELOW:
// The following variable defines whether links are opened in a new window
// (1 = Yes, 0 = No)
$OpenInNewWindow = "1";

// # DO NOT MODIFY ANYTHING ELSE BELOW THIS LINE!
// ----------------------------------------------
$BLKey = "***";

if(isset($_SERVER['SCRIPT_URI']) && strlen($_SERVER['SCRIPT_URI'])){
$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_URI'].((strlen($_SERVER['QUERY_STRING']))?'?'.$_SERVER['QUERY_STRING']:'');
}

if(!isset($_SERVER['REQUEST_URI']) || !strlen($_SERVER['REQUEST_URI'])){
$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'].((isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']))?'?'.$_SERVER['QUERY_STRING']:'');
}

$QueryString = "LinkUrl=".urlencode(((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on')?'https://':'http://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$QueryString .= "&Key=" .urlencode($BLKey);
$QueryString .= "&OpenInNewWindow=" .urlencode($OpenInNewWindow);


if(intval(get_cfg_var('allow_url_fopen')) && function_exists('readfile')) {
@readfile("http://www.backlinks.com/engine.php?".$QueryString);
}
elseif(intval(get_cfg_var('allow_url_fopen')) && function_exists('file')) {
if($content = @file("http://www.backlinks.com/engine.php?".$QueryString))
print @join('', $content);
}
elseif(function_exists('curl_init')) {
$ch = curl_init ("http://www.backlinks.com/engine.php?".$QueryString);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_exec ($ch);

if(curl_error($ch))
print "Error processing request";

curl_close ($ch);
}
else {
print "It appears that your web host has disabled all functions for handling remote pages and as a result the BackLinks software will not function on your web page. Please contact your web host for more information.";
}
?>

Update: one can do this:
vBulletin Admin area / Plugins & Products / Add New Plugin
As a Hook location you can use navbits_complete and it should load the php code on all pages of the forum including index. To show it only on thread pages, use "fetch_foruminfo"
Plugin is Active = yes
As a PHP code, do NOT enter <?php ?>, you should add code right away

ozzy47
08-03-2014, 12:32 PM
You can't use PHP in templates, you need to output the contents of the PHP to the templates via variables and such.

ozzy47
08-03-2014, 12:56 PM
I would also suggest asking for support from the backlinks site, as this is their code, they can tell you what to do.

Dave
08-03-2014, 01:00 PM
The easiest way is to make a plugin for this. Add the code to the plugin, bind it to the right hook location, assign it to a template variable and display it where needed.

postcd
08-06-2014, 07:11 AM
im really noob and not programmer to do it "plugin way", there would have to be some step by step tutorial to follow, and i dont want to waste whole afternoon doing it. Can i include some .php file into template?

cellarius
08-06-2014, 07:23 AM
No.

tbworld
08-06-2014, 07:28 AM
im really noob and not programmer to do it "plugin way", there would have to be some step by step tutorial to follow, and i dont want to waste whole afternoon doing it. Can i include some .php file into template?

Show and explain what you are trying to do, and attach necessary code. We can then give you some guidance, that might save you some time. There are no step-by-step guides to programming, just various technical articles and guidelines.

There is no method to include a PHP file directly into a template. One of the niceties of the vBulletin template system is it just might save you from yourself. Like including PHP in a way that you you shouldn't and allowing your board to be exploitable or compromised. :)

postcd
08-07-2014, 11:34 AM
i mentioned my code already before.
So what is the step by step easiest process to add it to the index.php page (forum.php) as i already mentioned

tbworld
08-07-2014, 05:19 PM
i mentioned my code already before.
So what is the step by step easiest process to add it to the index.php page (forum.php) as i already mentioned

I really need to get my eyes fixed. Sorry bud, somehow I missed the code being posted. I didn't realize that you posted the very well known "backlinks" code.

postcd
09-30-2014, 09:31 AM
So i know i cant add php code into template.

How can i let some php code be shown or executed on vbulletin forum, example index, header of footer? even dirty way? thanks

ozzy47
09-30-2014, 10:04 AM
Best thing to do is to study a mod from here that does something similar.

kh99
09-30-2014, 10:51 AM
Try this: create a plugin using hook location forumhome_start and code like this:

ob_start();
// insert code here without <?php or ?>
$backlinks = ob_get_contents();
ob_end_clean();
vB_Template::preRegister('FORUMHOME', array('backlinks' => $backlinks));

as the comment says, insert the code you posted above, but remove the <?php from the beginning and the ?> from the end.

Next, edit the FORUMHOME template and insert {vb:raw backlinks} where you want it to appear.

Edit: if you want step by step instructions for creating a plugin, see the vbulletin manual here: http://www.vbulletin.com/en/manual/ . First select your vb version from the dropdown menu, then search for "plugin" and select "Adding or Editing a Plugin" from the results. It's pretty easy, you just have to paste the code I posted, then paste yours in at the comment and remove the <?php and ?>, then fill in the other form fields as needed (see the manual page for that). If you don't know what to put for something, you can probably leave the default.

postcd
10-01-2014, 09:39 AM
if you want step by step instructions for creating a plugin, see the vbulletin manual here: http://www.vbulletin.com/en/manual/ .

good post, but cant find the page where it is described, google also no help, would be good if you update your post to link to manual or better paste it into the post.

kh99
10-01-2014, 09:42 AM
Yeah, sorry, it's just that the manual pages show in a frame so the browser url just goes to the manual home page. But I think this will work: http://www.vbulletin.com/docs/html/main/add_plugin

TheAdminMarket
10-01-2014, 10:13 AM
As most of other users said, you can't add PHP code to the template. But you can easily add templates in a PHP file. The results will be exactly what you want.

1.- Create 2 templates. One for header and one for footer:

a] For Header (is just as example):

<template name="php_header" templatetype="template" date="" username="" version="">
<![CDATA[
{vb:stylevar htmldoctype}
<html xmlns="http://www.w3.org/1999/xhtml" dir="{vb:stylevar textdirection}" lang="{vb:stylevar languagecode}" id="vbulletin_html">
<head>
<title>Your Title</title>
{vb:raw headinclude}
<vb:if condition="$vboptions['storecssasfile']">
{vb:cssfile forumhome.css, options.css, vbcms.css, widgets.css, sidebar.css, forumhome-rollup.css, usercp.css, additional.css}
<vb:else />
{vb:cssfile forumhome.css, options.css, vbcms.css, widgets.css, sidebar.css, forumhome-rollup.css, usercp.css, additional.css}
</vb:if>
<script type="text/javascript">
<!--
document.write('<script type="text/javascript" src="' + yuipath + '/animation/animation-min.js?v={vb:raw vboptions.simpleversion}"></script>');
var sidebar_align = 'right';
var content_container_margin = parseInt('{vb:math {vb:stylevar forum_sidebar_width}+{vb:math {vb:stylevar padding}*2}}');
var sidebar_width = parseInt('{vb:stylevar forum_sidebar_width}');
//-->
</script>
<script type="text/javascript" src="{vb:raw vboptions.bburl}/clientscript/vbulletin-sidebar.js?v={vb:raw vboptions.simpleversion}"></script>
</head>
<body>
{vb:raw header}
{vb:raw navbar}
<div class="blockbody">
]]></template>
a] For Footer (is just as example):

<template name="php_footer" templatetype="template" date="" username="" version="">
<![CDATA[
</div>
{vb:raw footer}
</body>
</html>
]]></template>
2.- In your PHP file (assuming that is a file which loads all vB functions that you need, add (again is an example):

// Header Block
$templater = vB_Template::create('php_header');
$templater->register_page_templates();
$navbits = construct_navbits(array("torget.php?" . $vbulletin->session->vars['sessionurl'] . ""=> "My Page"));
$navbar = render_navbar_template($navbits);
$templater->register('navbar', $navbar);
$header_block = $templater->render();
echo $header_block;
............Hera you can add any PHP code that you like..............

// Footer Block
$templater = vB_Template::create(php_footer');
$templater->register_page_templates();
$footer_block = $templater->render();
echo $footer_block;


Simple thing and works 100%. I build a complicated Ajax search form using this trick. And is 100% compatible with vBulletin meaning that you can use vboptions, vbphrase, datamanager etc.

Nick