Skip to content

Instantly share code, notes, and snippets.

@gatespace
Forked from hissy/my-adsense.php
Last active December 19, 2015 19:19
Show Gist options
  • Save gatespace/6005221 to your computer and use it in GitHub Desktop.
Save gatespace/6005221 to your computer and use it in GitHub Desktop.
自分のブログ用に改造してみた。 ウィジェット部分(常に)、is_singular がtrue なら <!-- more -->部分、本文の最後、かつモバイルでは小さいのが表示されるように。
<?php
/*
Plugin Name: My AdSense
Description: add adsense to my blog
Author: Takuro Hishikawa
Version: 0.1
Author URI: http://notnil-creative.com/
*/
// add adsbygoogle.js to footer
function adsbygoogle() {
wp_enqueue_script(
'adsbygoogle',
'http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js',
array(),
false,
true
);
}
add_action( 'wp_enqueue_scripts', 'adsbygoogle' );
// start adsbygoogle asynchronous
/*
function start_adsbygoogle() {
echo '<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>';
}
add_action('wp_footer', 'start_adsbygoogle', 100);
*/
// add widget
class My_Adsense_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'my-adsense-id',
'My AdSense Widget',
array(
'classname' => 'my-adsense-widget'
)
);
}
public function widget( $args, $instance ) {
echo '<ins class="adsbygoogle"
style="display:inline-block;width:250px;height:250px"
data-ad-client="ca-pub-1239126270965550"
data-ad-slot="9301387771"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>';
}
}
add_action( 'widgets_init', create_function( '', 'register_widget("My_Adsense_Widget");' ) );
// add ad to content in single
function add_adsense_to_content($content){
/* Blog コンテンツ */
$adTagsTopPC = <<< EOF
<ins class="adsbygoogle"
style="display:inline-block;width:468px;height:60px"
data-ad-client="ca-pub-1239126270965550"
data-ad-slot="6079368574"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
EOF;
/* Blogモバイル上 */
$adTagsTopMob = <<< EOF
<ins class="adsbygoogle"
style="display:inline-block;width:320px;height:50px"
data-ad-client="ca-pub-1239126270965550"
data-ad-slot="8753633373"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
EOF;
/* Blogコンテンツ下 */
$adTagsBottomPC = <<< EOF
<ins class="adsbygoogle"
style="display:inline-block;width:468px;height:60px"
data-ad-client="ca-pub-1239126270965550"
data-ad-slot="4463034573"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
EOF;
/* Blogモバイル下 */
$adTagsBottomMob = <<< EOF
<ins class="adsbygoogle"
style="display:inline-block;width:320px;height:50px"
data-ad-client="ca-pub-1239126270965550"
data-ad-slot="4323433773"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
EOF;
$adTagsTop = wp_is_mobile() ? $adTagsTopMob : $adTagsTopPC;
$adTagsTop = '<div class="ad"><small>スポンサーサイト</small><br>'.$adTagsTop."</div>\n";
$adTagsBottom = wp_is_mobile() ? $adTagsBottomMob : $adTagsBottomPC;
$adTagsBottom = '<div class="ad"><small>スポンサーサイト</small><br>'.$adTagsBottom."</div>\n";
$content = preg_replace('/<span id="more-[0-9]+"><\/span>/', $adTagsTop, $content);
if ( is_singular() ) {
$content .= $adTagsBottom;
}
return $content;
}
add_filter('the_content', 'add_adsense_to_content', 9999);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment