[WP] 포스트 상하단에 애드센스 광고 노출 플러그인
워드프레스 포스트 상하단에 구글 애드센스 광고를 출력하는 플러그인이다. 애드센스 광고 설정 기능은 제공하지 않는 단순한 기능의 플러그인으로 플러그인 적용 전 post-adsense.php 파일에서 자신의 애드센스 광고 설정을 해야 정상적으로 광고가 노출된다.
플러그인 : post-adsense.zip
<?php
/**
 * @package Post Adsense
 * @version 1.0
 */
/*
Plugin Name: Post Adsense
Plugin URI: https://chicpro.dev/
Description: This plugin prints Adsense Ads.
Author: Seongho Jang
Version: 1.0
Author URI: https://chicpro.dev/
*/
// Register js and style sheet.
add_action( 'wp_enqueue_scripts', 'register_post_adsense_style', 100 );
add_action( 'wp_enqueue_scripts', 'register_post_adsense_js', 100 );
/**
 * Register adsense Js
 */
function register_post_adsense_js() {
    wp_register_script('post-adsense-js', '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js');
    wp_enqueue_script('post-adsense-js');
}
/**
 * Register style sheet.
 */
function register_post_adsense_style() {
    wp_register_style( 'post-adsense-style', plugins_url( 'post-adsense/style.css' ) );
    wp_enqueue_style( 'post-adsense-style' );
}
add_filter ('the_content', 'post_adsense_print', 100);
function post_adsense_print($content) {
    if(is_single()) {
        if(!$content)
            return $content;
        
        $top_ads = '<div id="adsense-post-top" class="adsense-post">
                        <ins class="adsbygoogle"
                            style="display:block"
                            data-ad-client="ca-pub-16166"
                            data-ad-slot="90868"
                            data-ad-format="auto"></ins>
                        <script>
                        (adsbygoogle = window.adsbygoogle || []).push({});
                        </script>
                    </div>';
        $btm_ads = '<div id="adsense-post-bottom" class="adsense-post">
                        <ins class="adsbygoogle"
                            style="display:block"
                            data-ad-client="ca-pub-16166"
                            data-ad-slot="27266"
                            data-ad-format="auto"></ins>
                    <script>
                    (adsbygoogle = window.adsbygoogle || []).push({});
                    </script>
                    </div>';
        
        $content = $top_ads . $content . $btm_ads;
    }
    return $content;
}
$top_ads와 $btm_ads 부분 에서 data-ad-client, data-ad-slot 값을 자신의 애드센스 설정으로 반드시 변경하셔야 합니다.
