Aubrey Portwood

WordPress — Stoicism — 🎾

Simple Google Adsense Code Insertion/Injection WordPress Plugin using Custom Fields/Meta

| Permalink

[iframe width=”100%” height=”349″ src=”http://www.youtube.com/embed/tKUq5TZFeCU”]

Today I was working on adding a Google AdSense Ad into a post on the site, and I couldn’t (easily) find a WordPress Plugin that would let me do that easily; so, as always, I wrote one.

[adsense metakey=”my-cool-adsense-ad”]

To use add the shortcode to your post. (Not here, because it would actually try and insert something, please see video on these steps.)

Put your adsense code (that you copy from Google Adsense) in a Custom Field and call it whatever you want (called adsense-ad-1 here). The shortcode will pull in the AdSense code from that Custom Field in the location you put the shortcode in your post.

Enjoy.

Simple Google AdSense Code Insertion Plugin [zip]

[php wraplines=”true”]
/*
Plugin Name: Simple Google Adsense Code Injector Shortcode
Plugin URI: http://aubreypwd.com/
Description: This easy shortcode will allow you to insert your adsense code easily into your posts. Just put your adsense code betweent <code>adsense /adsense</code>.
Version: 0.1
Author: Aubrey Island
Author Email: enethrie@gmail.com
Author URI: http://aubreypwd.com
*/

function adsense_hand_enji( $atts, $content = null ) {
global $post;
extract( shortcode_atts( array(
‘metakey’ => ”,
//’attr_2′ => ‘attribute 2 default’,
), $atts ) );

$code= get_post_meta($post->ID, $metakey, true);

return $code;

}

add_shortcode( ‘adsense’, ‘adsense_hand_enji’ );
[/php]

Leave a Comment