Add Adsense to Your WordPress Post using Shortcodes

Update 12/2015: I’m no longer updating this post so I can’t guarantee compatibility with latest version of WordPress. Test on your Localhost first before implimenting it on your live site!

[adsensetl][kuadika]I enjoy coding. That’s why I love WordPress, and learning something new everyday is an added bonus. Recently, I’ve been working on a project to implement interactive transcripts to videos. This is a feature that our clients would appreciate. Anyway, there was an hurdle in implementing interactive transcripts into a WordPress blog and I turned to the WordPress Shortcode API to find a solution.

What I realized was that the WordPress Shortcode API is very easy to learn and work with. And although it didn’t provide a simple solution to the problem I had with the interactive transcripts project, I did realize how powerful it is. Immediately I could tell that the Shortcode API was going to make blogging much easier.

WordPress Shortcodes are akin to macros in MS Word, in that you write a little bit of code and it helps you do all the heavy lifting. Especially for repetative tasks. For instance, on every blog post I add a Cleanprint button that my leaders can use to instantly print the post. Cleanprint is a great way to save paper and be green while having a hardcopy of a post that you can use for reference. But it tedious for me to copy paste the code on every post.

With shortcodes, all I need to do is add the shortcode to my post and voila, a Cleanprint button magically appears. Shortcodes are a great way to also embed Adsense code to your post. How? Let me show you in four easy steps.

The Steps

Step 1: Write the code.

Open Notepad or which ever text editor you use to write code. (Notepad++ is a great). Copy paste the following code into your text editor.

function onania() {
return ‘ ’;
add_shortcode(‘adsensefl’, ‘onania’);

Step 2: Get your Adsense Embend code from your Google Adsense Account

Log into your Adsense account and get the embend code. Copy paste this code inbetween the inverted  commas (‘’) in the code you wrote in Step 1.

Step 3: Copy paste the code into your function.php

Copy paste the code you’ve written in Step 2 to your template’s function.php. This is probably the most difficult step. You will need to find your template’s functions.php, which is located in /wp-content/themes/your-theme/. You’ll then need to paste the code inside the closing >? php tag.

Step 4: Embed Adsense to your WordPress post

In Step 1, we created your Adsense shortcode which is [adsensefl] that calls the function onania. Add this shortcode to any part of your post to embed Adsense.


You can change the name of the function onania () and the shortcode name ‘adsensefl’ to whatever you like. Just make sure it easy for you to remember.

You can add a  Div and some styling to your code. Here is an example of the code I now use to embed Adsense to my posts:

function onania() {
// wrap text around adsense with a div

<div style=”float: left; margin-right: 10px;”>
[Your Adsense embed code] </div>

add_shortcode(‘adsensetl’, ‘onania’);

And here is the code that I use to embed the Cleanprint button.

function kuadikagutheru () {

return <div style="float: right; margin-top: -10px; margin-left: 5px;"
[Clean print code] </div>

add_shortcode (‘kudika’, ‘kuadikagutheru’);

The shortcode for Cleanprint is [kudika].

Hope this is of use to all of you WordPress bloggers. Are you using shortcodes on your blog? Why not?

2 responses to “Add Adsense to Your WordPress Post using Shortcodes”

  1. Tim Avatar

    Don’t you need to add the <> to the div tags?

    1. Kongo Avatar

      You sure do. So:

      <div style=”float: left; margin-right: 10px;”>
      [Your Adsense embed code] </div>

      I’ve updated the post. Thanks for the clarification.

Leave a Reply

Your email address will not be published. Required fields are marked *