Showing posts with label body. Show all posts
Showing posts with label body. Show all posts

Thursday, October 20, 2011

How to add Infolinks/Kontera code in Drupal site

Although Infolinks and Kontera do give free modules to help as integrate their javascript code in our site but they don't seem to work.
I don't know how Infolinks calls its module a stable one when it started giving problems in my taxonomy breadcrumbs in my site.

Here are the easy steps to add Javascript Ad code in your site.


How to add javascript code before end of Body tag in Drupal

Do you want to integrate Javascript/Jquery etc code just before the end of body tag in
Drupal.
I've worked on Drupal 6 and there does not seem to be any module to do it.

But you can very simply do it by adding in the theme page.tpl.php.
Don't use drupal_add_js in page.tpl.php as it does not seem to work. I added even in template.php but did not work.


Just go to <your-theme>/page.tpl.php and add the desired code
just before this code at the bottom of the file:
<?php print $closure ?>

</body>

</html>

Now just go to admin/build/themes and click configure on the default theme and Save it once so that your site's theme cache is recreated.

No need to display Infolink code for Admin

If you don't want the infolink code to be displayed for admin then wrap the code as in below. If you don't want to display only for anonymous users then simply check $user->uid>0 instead of :

global $user;

if( $user->uid != 1)

{

//Your javascript code

}

Integration for multi-site setup

Do you have same files sharing multiple drupal sites? Then integrate the Javascript code in the same page.tpl.php using the following technique:

<?php

{

global $base_root;

global $user;

if( $user->uid != 1  &&  strstr($base_root,"site1.com") )

{

print<<<EOT

<script type="text/javascript">
 

  var infolink_pid = 325111;
  

var infolink_wsid = 4;

</script>
<script type="text/javascript" src="http://resources.infolinks.com/js/infolinks_main.js"></script>
EOT;

}

else if( $user->uid != 1  && strstr($base_root,"site2.com") )

{

print<<<EOT

<script type="text/javascript">
 

  var infolink_pid = 325111;

   var infolink_wsid = 5;

</script>

<script type="text/javascript" src="http://resources.infolinks.com/js/infolinks_main.js"></script>
EOT;

}

else if( $user->uid != 1  &&  strstr($base_root,"site3.com") )

{

print<<<EOT
<script type="text/javascript">
   var infolink_pid = 325111;

   var infolink_wsid = 3;

</script>
<script type="text/javascript" src="http://resources.infolinks.com/js/infolinks_main.js"></script>
EOT;

}


}

?>