メインメニューを開く

MediaWikiにGoogle AdSenseを追加してみた/MobileFrontend

2019年7月26日 (金) 08:15時点におけるRin-scrooge (トーク | 投稿記録)による版 (→‎やり方)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)

MediaWikiのスマホサイトはMobileFrontendってextension(拡張)で実現しているので、前回の「MediaWikiにGoogle AdSenseを追加してみた」をしても、スマホの画面にアドセンスは追加されないんだよ。なので、MibleFrontend用のアドセンスの追加方法を別途あげておくよ。(MobileFrontendのインストールは「MediaWikiに携帯用の画面を追加する」を見てね)

やり方

基本的な流れは「MediaWikiにGoogle AdSenseを追加してみた」の記事の後ろに追加する方法と基本的に同じなんだよ。

  1. 以下のディレクトリから「MinervaTemplate.php」ってファイルを探してね。
    【MediaWikiをインストールしたディレクトリ】/extensions/【MobileFrontendをインストールしたディレクトリ】/includes/skins
    

    僕の場合は以下の通りになるよ。

    【MediaWikiをインストールしたディレクトリ】/extensions/MobileFrontend/includes/skins
    
  2. 「MinervaTemplate.php」から以下の行を探してね。
    protected function renderContentWrapper( $data ) {
    	$isSpecialPage = $this->getSkin()->getTitle()->isSpecialPage();
    	?>
    	<div id="content_wrapper">
    		<?php
    			if ( !$isSpecialPage ) {
    				echo $data['prebodytext'];
    				// FIXME: Temporary solution until we have design
    				if ( isset( $data['_old_revision_warning'] ) ) {
    					echo $data['_old_revision_warning'];
    				} else {
    					$this->renderPageActions( $data );
    				}
    			}
    		?>
    		<?php if ( !$data[ 'unstyledContent' ] ) { ?>
    		<div id="content" class="content">
    			<?php
    				if ( isset( $data['subject-page'] ) ) {
    					echo $data['subject-page'];
    				}
    				echo $data[ 'bodytext' ];
    				$this->renderMetaSections();
    				$this->renderHistoryLink( $data );
    			?>
    		</div>
    		<?php } else {
    			echo $data[ 'bodytext' ];
    		} ?>
    	</div>
    	<?php
    }
    
  3. この部分を以下の通り変更するよ。(思い切って<div id="content_wrapper">タグの上下に入れてやったよ。)
    protected function renderContentWrapper( $data ) {
    	$isSpecialPage = $this->getSkin()->getTitle()->isSpecialPage();
    	?>
    	<script async src="xxxx"></script>
    		<!-- AdSense_mobile -->
    		<ins class="adsbygoogle"
    		     style="display:inline-block;width:300px;height:250px"
    		     data-ad-client="xxxx"
    		     data-ad-slot="xxxx"></ins>
    		<script>
    		(adsbygoogle = window.adsbygoogle || []).push({});
    	</script>
    	<div id="content_wrapper">
    		<?php
    			if ( !$isSpecialPage ) {
    				echo $data['prebodytext'];
    				// FIXME: Temporary solution until we have design
    				if ( isset( $data['_old_revision_warning'] ) ) {
    					echo $data['_old_revision_warning'];
    				} else {
    					$this->renderPageActions( $data );
    				}
    			}
    		?>
    		<?php if ( !$data[ 'unstyledContent' ] ) { ?>
    		<div id="content" class="content">
    			<?php
    				if ( isset( $data['subject-page'] ) ) {
    					echo $data['subject-page'];
    				}
    				echo $data[ 'bodytext' ];
    				$this->renderMetaSections();
    				$this->renderHistoryLink( $data );
    			?>
    		</div>
    		<?php } else {
    			echo $data[ 'bodytext' ];
    		} ?>
    	</div>
    	<script async src="xxxx"></script>
    		<!-- AdSense_mobile -->
    		<ins class="adsbygoogle"
    		     style="display:inline-block;width:300px;height:250px"
    		     data-ad-client="xxxx"
    		     data-ad-slot="xxxx"></ins>
    		<script>
    		(adsbygoogle = window.adsbygoogle || []).push({});
    	</script>
    	<?php
    }
    
  4. スマホから画面を見て確認してね