Add Google Adsense after first header in DokuWiki

Use plugin

There's a Google Adsense plugin for dokuwiki available located at this website; It requires inserting codes to each and might not be very practical, and this is especially for a wiki system where everyone can actually create and edit pages.

Edit parser

The other method is to go to the core of Dokuwiki itself, and edit the parser. In this case, we'll make Google Adsense ad to appear after the H1 heading, just as how it normally appear in blogs.

The trick is to edit a function called header in <dokuwiki installation directory>/inc/parser/xhtml.php. Originally there's a line in the function as the following;

$this->doc .= "</a></h$level>".DOKU_LF;

What the line do is to add the right <Hn> tag to each heading levels as defined in Dokuwiki's syntax. What we need to do is to tweak it to add our Google Adsense code if the defined level is '1', just as the following;

        if ($level==1)

                $this->doc .= "</a></h$level>";
                $this->doc .= '

<script type="text/javascript"><!--
google_ad_client = "pub-2726248188023431";
/* techrecipes - header */
google_ad_slot = "4924174629";
google_ad_width = 336;
google_ad_height = 280;
<script type="text/javascript"
<br />

<br />';

                $this->doc .= DOKU_LF;

        } else
                $this->doc .= "</a></h$level>".DOKU_LF;

Sign up for Object Oriented PHP & MVC and many other great courses on Udemy to learn more.

Written by Mohd Shakir Zakaria. Last updated on 2019-02-12