Topic on Project:Support desk

Where do I put Google AdSense code in Vector.php for a banner ad at the top?

15
Lieutenant S. Reznov (talkcontribs)

Here's an excerpt of the code:

		// Output HTML Page
		$this->html( 'headelement' );
?>
		<div id="mw-page-base" class="noprint"></div>
		<div id="mw-head-base" class="noprint"></div>
		<!-- content -->
		<div id="content" class="mw-body" role="main">
			<a id="top"></a>
			<div id="mw-js-message" style="display:none;"<?php $this->html( 'userlangattributes' ) ?>></div>
			<?php if ( $this->data['sitenotice'] ): ?>
<!-- Google AdSense -->
<script type="text/javascript"><!--
google_ad_client = "ca-pub-xxxxxxxxxxxxxxxxxxx";
/* Banner Ad */
google_ad_slot = "xxxxxxxxxxxxxx";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<!-- /Google AdSense -->
			<!-- sitenotice -->
			<div id="siteNotice"><?php $this->html( 'sitenotice' ) ?></div>
			<!-- /sitenotice -->
			<?php endif; ?>
			<!-- firstHeading -->
			<h1 id="firstHeading" class="firstHeading" lang="<?php
				$this->data['pageLanguage'] = $this->getSkin()->getTitle()->getPageViewLanguage()->getCode();
				$this->html( 'pageLanguage' );
			?>"><span dir="auto"><?php $this->html( 'title' ) ?></span></h1>
			<!-- /firstHeading -->
			<!-- bodyContent -->
			<div id="bodyContent">

http://community.wikihub.ssu.lt/WikiHub

This post was posted by Lieutenant S. Reznov, but signed as Inquisitor Ehrenstein.

88.130.120.37 (talkcontribs)

Yes, if it's working... :-/

Lieutenant S. Reznov (talkcontribs)
88.130.109.50 (talkcontribs)

Why?

IAlex (talkcontribs)

It's inside the if ( $this->data['sitenotice'] ): block. If you want it to show even where there are no site notices, you need to move it outside of that block.

Lieutenant S. Reznov (talkcontribs)

Thanks. Yeah, I just realized that when it seemed to only display when there was content on the sitenotice page. I fixed it.

This post was posted by Lieutenant S. Reznov, but signed as Inquisitor Ehrenstein.

Krinkle (talkcontribs)

Don't hack core files (Vector.php in this case).

There's many routes to take that don't involve hacking core, here's two that should work fine for you:

These hooks would be created in your LocalSettings.php file.

Lieutenant S. Reznov (talkcontribs)

I used this:

public static function onBeforePageDisplay( OutputPage &$out, Skin &$skin ) {
<script type="text/javascript"><!--
google_ad_client = "ca-pub-xxxxxxxxxxxxxx";
/* Banner Ad */
google_ad_slot = "xxxxxxxxxx";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
}

$wgHooks['BeforePageDisplay'][] = 'OutputPage::addInlineScript';

Could you please show me how I should set it up?

This post was posted by Lieutenant S. Reznov, but signed as Inquisitor Ehrenstein.

88.130.64.43 (talkcontribs)

What should the code, which you posted, do? Where did you put these lines? There should be functions in LocalSettings.php, but the code you posted is JavaScript, or - as a PHP programmer would say: A syntax error. And how should things work together? Your hook definition does not call the function, which you have defined. Looks all very strange...

Basically this should work - in LocalSettings.php to be sure:

$wgHooks['BeforePageDisplay'][] = 'addGoogleAdSense';

function addGoogleAdSense( OutputPage &$out, Skin &$skin ) {
  $adSenseCode = 'foo bar'; // add the content of the script tag (not also the script tag itself!) of your JavaScript here.
  
  $out->addInlineScript( $adSenseCode );
  
  // Do the same again, if you still need another script...
  // After you did it wrong, think about it yourself and fix it.
  
  return TRUE; // ohh yeah!
}

That should give you an idea of how to really use hooks.

Lieutenant S. Reznov (talkcontribs)

Thanks. I had no idea what to do before. I'll try this.

EDIT

I just used this, but it didn't work. If I didn't give enough information before, it's being used with Vector and I'd like it to display like the banner here.

$wgHooks['BeforePageDisplay'][] = 'addGoogleAdSense';

function addGoogleAdSense( OutputPage &$out, Skin &$skin ) {
  $adSenseCode = '
<script type="text/javascript"><!--
google_ad_client = "ca-pub-xxxxxxxxxxxxxxxxx";
/* Banner Ad */
google_ad_slot = "xxxxxxxxxxx";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
';

$out->addInlineScript( $adSenseCode );

  return TRUE;
}

EDIT

I saw that I put in too much. I'm not supposed to include the tag.

This post was posted by Lieutenant S. Reznov, but signed as Inquisitor Ehrenstein.

Lieutenant S. Reznov (talkcontribs)

I just tried this but it's still not working.

EDIT

Should I do each part of the code separately?

$wgHooks['BeforePageDisplay'][] = 'addGoogleAdSense';

function addGoogleAdSense( OutputPage &$out, Skin &$skin ) {
  $adSenseCode = '
google_ad_client = "ca-pub-xxxxxxxxxxxxx";
/* Banner Ad */
google_ad_slot = "xxxxxxxxxx";
google_ad_width = 728;
google_ad_height = 90;

src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
';

$out->addInlineScript( $adSenseCode );

  return TRUE;
}

{{LQT post imported with different signature user|authorUser=Lieutenant S. Reznov|signatureUser=Inquisitor Ehrenstein}}
Lieutenant S. Reznov (talkcontribs)

I just figured this out using addHTML() as addInlineScript() doesn't work because the whole second part of the script is inside the script tag, which isn't supposed to be included, so there's then nothing to include.

The problem is that it appears at the bottom of the page, when it should appear at the top.

Adds at the end of an article are useful, but this isn't what I need. For one reason, I need to included a hosted by WikiHub and report abuse link.

EDIT

By using prependHTML() I was able to get it to appear at the top, but it's below the title rather than above it, which would be preferable.

This post was posted by Lieutenant S. Reznov, but signed as Inquisitor Ehrenstein.

Krinkle (talkcontribs)

I have to conclude from above attempts that you seem to have little to no understanding of HTML or PHP. Obvious syntax conflicts with adding raw HTML inside a PHP function body, passing HTML as a string where javascript code is expected, and now passing in half of an HTML script tag (src="..." >) inside of javascript.

I could give the entire answer, but I wouldn't feel comfortable leaving that in your hands to further maintain in the future. It also seems moot as you wouldn't have learned anything and wouldn't know what to do with it further.

If someone asked you to do this, I'd recommend going back and telling them to ask someone else do this, because that's what it would be if I gave the full answer to you now. The internet would be the indefinite proxy for whatever it is you are supposed to do.

If this for an adventure of your own making, I'd recommend you make a choice. Either drop it, or take it on all the way and start by taking a step back and first learning HTML and PHP.

You're welcome.

Lieutenant S. Reznov (talkcontribs)

I have HTML fairly well and a basic understanding of PHP. I was basically just trying things quickly without taking the time to understand them before. I see the problem with the partial tag and the src there.

This post was posted by Lieutenant S. Reznov, but signed as Inquisitor Ehrenstein.

Lieutenant S. Reznov (talkcontribs)