Create shortcode for Google Adsense embed code in WordPress

Inserting ads in between posts is one of the best ways to get clicks and get visitor attention.

There are many plugins out there to dynamically insert ads in between/inline paragraphs in a post.

But what if you want to keep it simple to manually insert ads in posts and pages as and when needed?

The normal way:

Copy the ad code and paste it into the editor.

This works but the problem is if you want to change the ad code in the future, then you have to edit all the posts one by one. This is inconvenient.

The Shortcodes way:

We can create “shortcodes” for such ad code snippets and then use the shortcode. Shortcoder is one such plugin to create shortcodes for HTML, CSS, and JavaScript code snippets.

Installing Shortcoder

Shortcoder is a free plugin available in the WordPress plugin repository. Go to your WordPress admin dashboard.

Head over to Plugins > Add new page. Search for “Shortcoder”, click install, and activate the plugin.

Installing Shortcoder plugin
Installing Shortcoder plugin

Creating Shortcodes for Ad snippets

Go Shortcoder > “Create shortcode” page.

Give a name for the shortcode. Let’s name it “adsense-inline”. Our shortcode will be [sc name="adsense-inline"] now.

In the shortcode content, paste the Google Adsense code.

Creating the shortcode for Google adsense code
Creating the shortcode for Google AdSense code

Save the shortcode and that’s it.

You have now created a Shortcode for the AdSense code. You can now use this shortcode inside posts and pages.

[sc name="adsense-inline"]

It would be then replaced with the AdSense code you have set as shortcode content.

You can create multiple shortcodes for code snippets like this.

Hiding the Shortcode for Administrators

What if you as an administrator do not want to see the ads you are displaying via the shortcodes you created above?

Shortcoder even provides an option to “Disable shortcode for administrators” on the shortcode edit page.

Now when you are logged in to your site and when you visit a page where this shortcode is present, it won’t be executed.

Let’s say you want to temporarily disable executing the shortcode for all users. You can select “Temporarily disable shortcode” to “Yes” and it will stop displaying everywhere for all. This is beneficial in some use cases.

Conclusion

As we saw it is very easy to create shortcodes for Google Adsense code snippets. Check out Shortcoder plugin if you have similar use cases to reuse simple static code snippets.

If you have any comments, feel free to share them in the comments section below.

Subscribe for updates

Get updates on the WordPress plugins, tips and tricks to enhance your WordPress experience. No spam. View newsletter

Add your comment No Comments so far