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
- Go to your Wordpress Theme Editor
- Select Theme to Edit in Wordpress Theme Editor
- Edit your Theme Functions in your Wordpress Theme Editor
- Scroll Down to the Bottom of your Theme Functions File
- Insert Code to Create Adsense Shortcode in WordPress
- Create a New Post in WordPress
- Use the Adsense Shortcode In Your Post
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
Hope that helps,