How to Create Adsense Shortcode in WordPress

by Elaine on July 19, 2013

I decided a few days ago, that I didn’t like how the Adsense plugins worked.  They never put the Adsense ads where I wanted them to show up and I wanted more power and control over it.  I didn’t want to always be wary of upgrading the plugin as well and if they put some hidden Adsense code to donate money back over to the programmers.  So I decided to create my own Adsense Shortcodes and insert the shortcodes into my articles myself whenever and wherever I wanted them.   So below is a Wordpress tutorial on how to make Adsense Shortcodes.  It’s very easy, I just detailed every step from adding the code into your Theme Functions to using the Shortcode in your posts.

Prerequisites:

  • You must have an Adsense Account
  • You have your Adsense code copied into your clipboard

Overview of How To Create Adsense Shortcodes in Wordpress

  1. Go to your Wordpress Theme Editor
  2. Select Theme to Edit in Wordpress Theme Editor
  3. Edit your Theme Functions in your Wordpress Theme Editor
  4. Scroll Down to the Bottom of your Theme Functions File
  5. Insert Code to Create Adsense Shortcode in WordPress
  6. Create a New Post in WordPress
  7. Use the Adsense Shortcode In Your Post
  8. View the Test Post with Adsense Shortcode

Steps:

1.  Go to your WordPress Theme Editor

You can edit your WordPress theme html files and css files in the theme editor.  Be sure to take a backup of anything you make a change to because any errors can bring down your site.

cmsmind wordpress tutorial appearances editor 1 How to Create Adsense Shortcode in WordPress
WordPress Tutorial – Appearance Editor
  • Login to your WordPress Administrator backend
  • Click on ‘Appearance’
  • Click on ‘Editor

2.  Select Theme to Edit in WordPress Theme Editor

If you have many themes installed, you want to make sure you are editing the correct theme.  Make sure you choose your theme from the dropdown to make sure you are editing the correct set of files.

wordpress select theme to edit appearance editor How to Create Adsense Shortcode in WordPress
WordPress Tutorial – Select Theme to Edit
  • In Theme Editor
  • In the ‘Select Theme to edit’ drop down box
  • Select the WordPress Theme that is currently activated on your site

 3.  Edit your Theme Functions in your WordPress Theme Editor

Along the right navigation, you will see a list of all the files you can edit.  These are php files that are the core of your website.   Find the Theme Functions file also called the functions.php file.

cmsmind wordpress tutorial edit theme functions 2 How to Create Adsense Shortcode in WordPress
WordPress Tutorial – Edit Theme Functions – functions.php
  • Along right navigation
  • Click on Theme Functions (functions.php)

4.  Scroll Down to the Bottom of your Theme Functions File

Take a backup of your Theme Functions file.  You can copy the whole file into your clipboard and save it as a backup.  That’s what I did.  I brought down my site a couple times because I made some mistakes.  I SSH’ed back into my site to fix the problems.  Make sure you copy everything exactly, otherwise you might bring down your site as well.  Here we are just scrolling down to the bottom of the file, so we can prepare to add in the Adsense Shortcode code.

cmsmind wordpress tutorial edit theme functions 3 How to Create Adsense Shortcode in WordPress
WordPress Tutorial – Edit your Theme Functions File
  • Copy all file contents and paste to the clipboard as a backup
  • Scroll down to the bottom of the functions.php file

5.  Insert Code to Create Adsense Shortcode in WordPress

Insert your code to create your Adsense Shortcode here.

cmsmind wordpress tutorial edit theme functions add adsense shortcode 4 How to Create Adsense Shortcode in WordPress
WordPress Tutorial – Create Adsense Shortcode
  • Copy the below code into your Theme Functions Editor:

/*———————————————————————————–*/
/* Adsense 300×250 box media
/*———————————————————————————–*/
function get_adsense300250i($atts) {
return ‘

/*——Paste your adsense code here——*/

‘;
}
add_shortcode(‘adsense300i’, ‘get_adsense300250i’);

  • Replace the red text in the code above with your own Adsense code.  You can see what my shortcode code looks like in the image above
  • A little breakdown:
    • get_adsense300250i = You can change this name, I just decided to call my little program this name
    • adsense300i = This is the shortcode text, so make it simple and easy to remember
    • add_shortcode(‘adsense300i’, ‘get_adsense300250i’) = Make sure the get_adsense300250i name matches your little program name
  • Click on ‘Update File

6.  Create a New Post in WordPress

Create a new post in WordPress to test out the new shortcode.  You can add this shortcode to existing posts as well.

cmsmind wordpress tutorial add new post 5 How to Create Adsense Shortcode in WordPress
WordPress Tutorial – Add New Post
  • Click on ‘Posts’, ‘Add New

7.  Use the Adsense Shortcode In Your Post

You can use this Adsense Shortcode in Visual or HTML view.  It will work both ways.

cmsmind wordpress tutorial add adsense shortcode into post 6 How to Create Adsense Shortcode in WordPress
WordPress Tutorial – Add Adsense Shortcode in Post
  • In order to add your shortcode, you must surround it with square brackets
  • You can add the shortcode in Visual or Text view with square brackets and it will work
  • So type:

[adsense300i] This is a post with my adsense shortcode.  It should be a 300×250 box media ad.

  • Save and View the post

8.  View the Test Post with Adsense Shortcode

At first when you view your post, the ad might take a while to show up.  You will see a big white space where the ad is supposed to be.  After you refresh a few times, you will see your ad show up.

cmsmind wordpress tutorial see adsense shortcode into post 7 How to Create Adsense Shortcode in WordPress
WordPress Tutorial – See Adsense Shortcode in Post

Hope that helps,

Elaine

 How to Create Adsense Shortcode in WordPress

Elaine

Elaine is the founder of CmsMind, an online blog providing Joomla and WordPress tutorials. Elaine lives in Toronto and when she is not online, she is either Marathon and Triathlon training or travelling and racing somewhere in the world. Find her also at: Save Money, Premium Themes and Templates, and at Webhosting Tips.

More Posts - Website

Leave a Comment

Captcha Captcha Reload

Older Post:

Newer Post: