Ad Inserter


A simple yet powerful plugin to insert any ad or code into WordPress. Perfect for all kinds of ads. Simply enter any ad or HTML/Javascript/PHP code and select where and how you want to insert it.


  • 16 code blocks
  • Syntax highlighting editor
  • Code preview with visual CSS editor
  • Automatic insertion: before/after post, content, paragraph or excerpt
  • Insertion exceptions for posts and pages
  • Manual insertion: widgets, shortcodes, PHP function call
  • Custom block alignment and style
  • Clearance options to avoid insertion near images or headers
  • PHP code processing
  • Ad rotation (server-side and client-side – works with caching)
  • Device detection (server-side and client-side, 3 custom viewports)
  • Black/White-list categories, tags, post IDs, urls, url parameters, referers
  • Simple troubleshooting with many debugging functions
  • Function to visualize inserted code blocks
  • Function to visualize available insertion positions
  • Function to visualize HTML tags

Ad Inserter plugin is simple to use but has many features. Please read user manual to get the most of it.

  • Check Ad Inserter documentation page for detailed description of all the features
  • Download PDF user guide for Ad Inserter: go to Ad Inserter Pro page and below you can find button for free download of Ad Inserter User Guide

Endorsed by Amazon

Amazon suggests to use Ad Inserter to add Native Shopping Ads to WordPress posts. Native Shopping Ads provide highly relevant and dynamic product recommendations in a stylishly designed and responsive ad unit that can be placed at the end of your content or within your content to create a more compelling visitor experience and shopping opportunity.

There is also Ad Inserter Pro if you need more:

  • 64 code blocks
  • GEO targeting (works also with caching)
  • Black/White-list IP addresses or countries
  • Scheduling with fallback option
  • Export and import of settings
  • Multisite options to limit settings on the sites
  • 6 viewports
  • Support via email

Quick Start

Few very important things you need to know in order to insert code and display some ad:

  • Code block is any code (for example AdSense ad) that has to be inserted (and displayed) at some position. Each code block can be configured to insert code at almost any position supported by WordPress
  • Enable and use at least one insertion option (Automatic insertion, Widget, Shortcode, PHP function call)
  • Enable insertion on at least one WordPress page type (Posts, Static pages, Homepage, Category pages, Search Pages, Archive pages)
  • For Posts and static pages select default value On all Posts / On all Static pages unless you really know what are you doing
  • If you don’t see inserted code block turn on debugging functions: Label inserted blocks, Show available positions for automatic insertion (Ad Inserter menu item in the WordPress toolbar on the top of every post/page)

Few typical settings are described on the FAQ page. Please make sure you have also read Docs page.
For more detailed instructions please read Ad Inserter documentation page.

Please support the plugin if you like it:


  • Settings for one code block (Before post). Up to 16 blocks can be configured (up to 64 in Ad Inserter Pro)
  • Alignment Left, None - None means default (usually left) aligned ad block with thin margin around
  • Alignment Right - Right aligned ad block with thin margin around
  • Alignment Center - Center aligned ad block with thin margin around
  • Alignment No Wrapping - Default (usually left) aligned ad block with no margin around
  • Alignment Custom CSS - Ad block with custom CSS (no margin around). You can use it for special effects (border, background, padding, margin, floating, etc.)
  • Alignment Float Left - Left aligned ad block with thin margin around wrapped with text on the right
  • Alignment Float Right - Right aligned ad block with thin margin around wrapped with text on the left
  • Complete settings for one code block (Before Paragraph)
  • Code preview with visual CSS editor
  • Code preview with visual CSS editor - highlighted code
  • Post / Page Ad Inserter Exceptions
  • Some Ad Inserter Pro features: IP address and country lists, Scheduling between dates with fallback
  • Ad Inserter settings
  • Visualization of HTML tags in post
  • Visualization of positions for automatic insertion in post
  • Visualization of inserted block in post


I have activated Ad Inserter. How can I use it?
  1. After activation, click “Settings / Ad Inserter” to access the settings page
  2. Put ad (or any other HTML/Javascript/PHP) code into the ad box
  3. Set automatic insertion option
  4. Enable at least one page type: Posts, Static pages, Homepage, Category pages, Search Pages, Archive pages (some insertion options don’t work on all page types)
  5. Save settings
Settings for widget
  • Nothing needed, just enter the code and save settings – widget is enabled by default
  • Go to Appearance / Widgets, drag Ad Inserter widget to the sidebar or any other widget position, select code block and click on Save
Settings for ad before the first paragraph on all posts
  • Automatic Insertion: Before Paragraph
  • On all Posts checked, other page types unchecked
  • Paragraph number: 1
Settings for ad before the last paragraph on all posts
  • Automatic Insertion: Before Paragraph
  • On all Posts checked, other page types unchecked
  • Paragraph number: 1
  • Count: From Bottom
Settings for contextual Amazon Native Shopping Ads
  • Automatic Insertion: After Content
  • On all Posts checked, other page types unchecked

Sign in to Amazon Associates, go to Widgets/Widget Source, choose ad type and set parameters.
For titles and search terms use tags. For example, the code below would display amazon products related to the post tags – check documentation for all possible tags.

<script type="text/javascript">
amzn_assoc_placement = "adunit0";
amzn_assoc_search_bar = "true";
amzn_assoc_tracking_id = "ad-inserter-20";
amzn_assoc_search_bar_position = "top";
amzn_assoc_ad_mode = "search";
amzn_assoc_ad_type = "smart";
amzn_assoc_marketplace = "amazon";
amzn_assoc_region = "US";
amzn_assoc_title = "Search Results from Amazon";
amzn_assoc_default_search_phrase = "{smart_tag}";
amzn_assoc_default_category = "All";
amzn_assoc_linkid = "cf1873f027a57f63cede634cfd444bea";
<script src="//"></script>

Use your own tracking id! Please note {smart_tag} as default search phrase. This makes this widget contextual.

Settings for ad before the second paragraph on all posts, but on the homepage only three ads in the first three post
  • Automatic Insertion: Before Paragraph
  • On all Posts checked, Homepage checked, other page types unchecked
  • Paragraph number: 2
  • Filter: 1, 2, 3 (in some cases 2, 4, 6 – depends on the theme and needs some testing)
Settings for ad wrapped with text of the third paragraph on all posts
  • Automatic Insertion: Before Paragraph
  • On all Posts checked, other page types unchecked
  • Block Alignment and Style: Float Left
  • Paragraph number: 3
Settings for centered ad in the middle of post paragraphs
  • Automatic Insertion: Before Paragraph
  • On all Posts checked, other page types unchecked
  • Block Alignment and Style: Center
  • Paragraph number: 0.5
Settings for ad above post excerpts on the Insurance category page
  • Automatic Insertion: Before Post
  • Category pages checked, other page types unchecked
  • Click Lists button to display lists
  • Categories: Insurance, White List checked
Settings for ad above the first and third post excerpts on the homepage
  • Automatic Insertion: Before Excerpt
  • Homepage checked, other page types unchecked
  • Filter: 1, 3
Settings for ad above the post excerpts on the Cars tag page
  • Automatic Insertion: Before Post
  • Archive pages checked, other page types unchecked
  • Click Lists button to display lists
  • Tags: Cars, White List checked
I wish to show ads side by side but not in the same block. How do I do this?

Configure block 1 and 2 with ads using:

  • Automatic Insertion: None
  • Block Alignment and Style: No Wrapping
  • Enable shortcode: checked

Configure block 3 with

[adinserter block="1"]
[adinserter block="2"]

Use block 3 to insert ads and make sure all 3 blocks are enabled for the same page types (Posts, Pages, Homepage, etc.).

I use After Content insertion position but the code is inserted after the stuff provided by other plugins. How can I insert directly after post content?

This happens because Ad Inserter processes posts last and therefore “sees” also content added by other plugins.

Try to set Ad Inserter plugin priority to 10 (early processing, Ad Inserter settings – tab *).

How can I replace deprecated tags {adinserter n} for manual insertion with new ones [adinserter block=”n”] in all posts?

Use Search Regex plugin to replace tags in all posts with few clicks. If you are not familiar with regular expressions simply use search and replace text for each code block. Use Replace to test replacements and when it works as expected use Replace & Save.

How can I add some text or title (e.g. Advertisement) above the ad?

If this is a sidebar widget then you can simply name the widget. In other cases you can add title HTML code above ad code. For example:



Change title tag according to the theme style.

I like the plugin. How can I support it?
How can I enable/disable ads on specific posts/pages?

There are two possible approaches.

  • Approach with page/post exceptions – use this one if there are many exceptions:
  1. Go to Ad Inserter settings page and define default insertion options for post/page.
  2. Enable automatic insertion on posts/pages and choose default display: On all, On all except selected or Only on selected.
  3. Go to post/page editor and open Ad Inserter Exceptions meta box below.
  4. Check default insertion options for wanted code blocks.
  5. Set exceptions for this post or page.
  • Approach with code block settings only – use this one if there are only few exceptions:
  1. Go to Ad Inserter settings page and define default insertion options for post/page.
  2. Enable automatic insertion On all Posts/Pages.
  3. Click on Lists, enter url (or space separated urls) for Urls, e.g. /permalink-url, and white-list or black-list it.
I’m using responsive theme. How can I show or hide some ads on mobile devices?

Select device types (desktop, tablet or phone) for which you need to show ads and enable only client-side device detection.

Which device type detection should I use?

In most cases you should use ONLY client-side detection. All code blocks are generated, however, they are displayed according to settings and browser’s screen width using CSS Media Queries. This works perfectly in responsive designs.

How can I disable ads for direct visitors?

Blacklist # as referer for visitors that enter web address directly into browser (no referer).
Blacklist as referer for visitors that browse your website
To blacklist both enter, #

How can I disable ads on error 404 page?

This is disabled by default. Uncheck Error 404 Page checkbox.

Is there a way to display country flag of the visitor?

Possible if you are using Ad Inserter Pro. In the header put the following code:

<link rel='stylesheet' href='/wp-content/plugins/ad-inserter/css/flags.css' type='text/css' media='all' />

In the code block use the following code for flag

<span class="flag-icon flag-icon-{country_iso2}" style="width: 400px; height: 300px;"></span>

Adjust width and height according to your needs. Please not that {country_iso2} tag works only server-side – it won’t work on cached pages.
Of course, you can also use static code for flags. For example, for United States:

<span class="flag-icon flag-icon-us" style="width: 400px; height: 300px;"></span>
How can I enable or disable ads on group of similar pages?

If those pages have something in common in the url (page address) then you can block them with url patterns.
Use * to define url pattern to whitelist (enable) or blacklist (disable).

  1. To filter all urls starting with /url-start use /url-start*
  2. To filter all urls that contain url-pattern use *url-pattern*
  3. To filter all urls ending with url-end use *url-end

For example, to exclude ads on pages that have /shop/ in url (page address) blacklist the following url: */shop/*

WARNING: Separate urls with SPACES.

How can I put an ad in the middle of the post regardless of the number of paragraphs?

Put 0.5 as paragraph number. Value between 0 and 1 means relative position in post or page (e.g. 0.3 means paragraph 30% from top or bottom)

I’d like to rotate my ad codes based on percentage, for example show one ad 75% of the time and another one 25% of the time. Is that possible?

Yes, simply create block with 4 ads separated with |rotate|: 3 times ad1 and 1 time ad2.

How can I show different ads to different visitors according to a url query parameter?

Use Url parameters List to black/white-list certain url parameters. Leave url parameter list empty and set it to Black list to show ads for all url.
You can specify either parameters or parameters with values. For example for url you can define url parameters ‘data, customer-id=22‘ to insert ad only for urls where there is data parameter and customer-id parameter with value 22.
Separate parameters with comma.

How can I use PHP code for code block?

Enter PHP code surrounded by PHP tags and check Process PHP.

<div style="width: 100%;">
Some HTML/Javascript code
<?php echo "PHP code by Ad Inserter"; ?>
How can I insert post category name into my ad code?
  1. Use {category} in the ad. This will be replaced with the post category name.
  2. You can also use
  • {title} – Title of the post
  • {short_title} – Short title (first 3 words) of the post title
  • {category} – Category of the post (or short title if there is no category)
  • {short_category} – First words before “,” or “and” of the category of the post (or short title if there is no category)
  • {tag} – The first tag or general tag if the post has no tags (works only inside posts)
  • {smart_tag} – Smart selection of post tag in the following order:
    • If there is no tag then the category is used;
    • If there is a two-word tag then it is used;
    • If the first tag is a substring of the second (or vice versa) then the first tag is not taken into account
    • If the first and second tags are single words then both words are used
    • First three words of the first tag
    • General tag
  • {search_query} – Search engine query that brought visitor to your website (supports Google, Yahoo, Bing and Ask search engines), {smart_tag} is used when there is no search query. You need to disable caching to use this tag. Please note that most search queries are now encrypted.
  • {author} – Post author username (works only inside posts)
  • {author_name} Post author name (works only inside posts)
How can I rotate few versions of the same ad?

Enter them into the ad box and separate them with |rotate| (vertical bars around text rotate). Ad Inserter will insert them randomly.

How can place ads below Read More tag?

Configure ad block with the following options:

  • Automatic Insertion: After Paragraph
  • Paragraph Number: 1
  • Count only paragraphs that CONTAIN: <span id="more-

Check source code of your website for proper “read more” tag.

How can I insert code block directly into template php file?

Enable PHP function adinserter for code block and call adinserter function with code block number as parameter.
Example for block 3:

<?php if (function_exists ('adinserter')) echo adinserter (3); ?>

This would generate code as defined for the code block number 3.

Center alignment does not work for some ads!

Some iframe ads can not be centered using standard approach so some additional code is needed to put them in the middle.
Simply wrap ad code in a div with some style e.g. left padding. Example:

<div style="padding-left: 200px;">
How can I rotate between different alignments so I can test an ad aligned to the right against an ad aligned to the left?

Set Block Alignment and Style to “No Wrapping” and create manual wrapping around both ads separated with |rotate|:

<div style="float: left; margin: 0 8px 8px 0;">


<div style="float: right; margin: 0 0 8px 8px;">


Read all 142 reviews



  • Added support for uppercase {country_ISO2} and lowercase {country_iso2} tag (Pro only)
  • Removed inclusion of dummy css and js file
  • Bug fix: Client-side dynamic blocks were not enabled if not using W3 Total Cache


  • Added support for client-side rotation (works with caching)
  • Added support for server-side rotation with W3 Total Cache
  • Added support for client-side country detection (works with caching, Pro only)
  • Added support for server-side country detection with W3 Total Cache (Pro only)
  • Added debugging functions to measure plugin processing time
  • Added option to black/white-list IP addresses (Pro only)
  • Added option for fallback code when scheduling between dates expires (Pro only)
  • On multisite installations Ad Inserter debug menu item on sites is available only if settings page is enabled
  • Added option for multisite installations to use Ad Inserter settings of main site for all blogs
  • Added flags to country list (Pro only)
  • Bug fix: Code preview did not work if WordPress was installed in a folder
  • Few minor bug fixes and cosmetic changes


  • Delayed display moved to Misc group
  • Added option for scheduling to insert code only between specified dates (Pro only)
  • Added option for Geo targeting (Pro only)
  • Few minor bug fixes and cosmetic changes


  • Added support to filter subpages created by the <!--nextpage--> tag
  • Added option to import block name (Pro only)
  • Cookie deleted only when it exists and debugging is disabled
  • Few minor bug fixes


  • Cookie created only when debugging is enabled
  • Few minor bug fixes


  • Bug fix: Cursor position always at the end of block name
  • State of debugging functions saved to cookie
  • Few minor bug fixes


  • Debugging functions in admin toolbar available only for administrators
  • Added option to hide debugging functions in admin toolbar
  • Added shortcode for debugger
  • Few minor bug fixes


  • Changed javascript version check to get plugin version from the HTML page
  • Added warning if old cached version of CSS file is loaded on the settings page
  • Added warning if version query parameter for js/css files is removed due to inappropriate caching


  • Bug fix: Shortcodes called by name were not displayed


  • Redesigned user interface
  • Added many debugging tools for easier troubleshooting
  • New feature: Code preview tool with visual CSS editor
  • New feature: Label inserted blocks
  • New feature: Show available positions for automatic insertion
  • New feature: Show HTML tags in posts/static pages
  • New feature: Log Ad Inserter processing
  • Improved loading speed of the settings page
  • Improved block insertion processing speed
  • Added support to avoid inserion near images, headers and other elements
  • Added option to avoid insertion in feeds
  • Added option to display code blocks only to administrators
  • Added option for publishig date check for display positions Before/After Content
  • Added option for server-side device check for header and footer code
  • Added option for maximum page/post words
  • Added option for maximum paragraph words
  • Added option to black/white-list post IDs
  • Added option to black/white-list url query parameters
  • Added warning if the settings page is blocked by ad blocker
  • Added warning if old cached version of javascript is loaded on the settings page
  • Added support for multisite installations to disable settings, widgets and exceptions on network sites (Pro only)
  • Block names can be edited by clicking on the name
  • Filters now work also on posts and single pages
  • CSS code for client-side detection moved to inline CSS
  • Bug fix: Minimum user roles for exception editing was not calculated properly
  • Bug fix: Server-side detection checkbox was not saved properly
  • Many other minor bug fixes, code improvements and cosmetic changes


  • Bug fix: Shortcodes did not ignore post/static page exceptions
  • Slightly redesigned user interface
  • Excerpt/Post number(s) renamed to Filter as it now works on all display positions
  • Widget setting removed from Automatic display to Manual display section
  • Added support to disable widgets (standalone checkbox in Manual display)
  • Added call counter/filter for widgets
  • Added support to edit CSS for predefined styles
  • Few other minor bug fixes, code improvements and cosmetic changes


  • Bug fix: Block code textarea was not escaped
  • Added checks for page types for shortcodes
  • Added support for Before/After Post position call counter/filter
  • Few minor cosmetic changes


  • Bug fix: Display on Homepage and other blog pages might get disabled – important if you were using PHP function call or shortcode (import of settings from 1.6.4)
  • Few minor cosmetic changes
  • Requirements changed to WordPress 4.0 or newer
  • Added initial support for Pro version


  • Fixed bug: Wrong counting of max insertions
  • Change: display position Before Title was renamed to Before Post
  • Added support for display position After Post
  • Added support for posts with no <p> tags (paragraphs separated with \r\n\r\n characters)
  • Added support for paragraph processing on homepage, category, archive and search pages
  • Added support for custom viewports
  • Added support for PHP function call counter
  • Added support to disable code block on error 404 pages
  • Added support to debug paragraph tags


  • Fixed bug: For shortcodes in posts the url was not checked
  • Optimizations for device detection


  • Removed deprecated code (fixes PHP 7 deprecated warnings)
  • Added support for paragraphs with div and other HTML tags (h1, h2, h3,…)


  • Removed deprecated code (fixes PHP Fatal error Call to a member function get_display_type)
  • Added support to change plugin processing priority


  • Fixed bug: For shortcodes in posts the date was not checked
  • Fixed error with some templates “Call to undefined method is_main_query()”
  • Added support for minumum number of page/post words for Before/After content display option
  • Added support for {author} and {author_name} tags


  • Added support for client-side device detection
  • Many code improvements
  • Improved plugin processing speed
  • Removed support for deprecated tags for manual insertion {adinserter n}
  • Few minor bug fixes


  • Fixed notice “Undefined index: adinserter_selected_block_” when saving page or post


  • Fixed notice “has_cap was called with an argument that is deprecated since version 2.0!”
  • Few minor bug fixes and code improvements
  • Added support to blacklist or whitelist url patterns: /url-start*. url-pattern, *url-end
  • Added support to define minimum number of words in paragraphs
  • Added support to define minimum user role for page/post Ad Inserter exceptions editing
  • Added support to limit insertions of individual code blocks
  • Added support to filter direct visits (no referer)


  • Fixed Security Vulnerability: Plugin was vulnerable to Cross-Site Scripting (XSS)
  • Few bug fixes and code improvements


  • Few bug fixes and code improvements
  • Added support to export and import all Ad Inserter settings


  • Many code optimizations and cosmetic changes
  • Header and Footer code blocks moved to settings tab (*)
  • Added support to process shortcodes of other plugins used in Ad Inserter code blocks
  • Added support to white-list or black-list individual urls
  • Added support to export and import settings for code blocks
  • Added support to specify excerpts for block insertion
  • Added support to specify text that must be present when counting paragraphs


  • Fixed Security Vulnerability: Plugin was vulnerable to a combination of CSRF/XSS attacks (credits to Kaustubh Padwad)
  • Fixed bug: In some cases deprecated widgets warning reported errors
  • Added support to white-list or black-list tags
  • Added support for category slugs in category list
  • Added support for relative paragraph positions
  • Added support for individual code block exceptions on post/page editor page
  • Added support for minimum number of words
  • Added support to disable syntax highlighting editor (to allow using copy/paste on mobile devices)


  • Fixed bug: Widget titles might be displayed at wrong sidebar positions
  • Change: Default code block CSS class name was changed from ad-inserter to code-block to prevent Ad Blockers from blocking Ad Inserter divs
  • Added warning message if deprecated widgets are used
  • Added support to display blocks on desktop + tablet and desktop + phone devices


  • Few fixes to solve plugin incompatibility issues
  • Added support to disable all ads on specific page


  • Added support to display blocks on all, desktop or mobile devices
  • Added support for new widgets API – one widget for all code blocks with multiple instances
  • Added support to change wrapping code CSS class name
  • Fixed bug: Display block N days after post is published was not working properly
  • Fixed bug: Display block after paragraph in some cases was not working propery


  • Fixed bug: Code blocks configured as widgets were not displayed properly on widgets admin page


  • Added support to skip paragraphs with specified text
  • Added position After paragraph
  • Added support for header and footer scripts
  • Added support for custom CSS styles
  • Added support to display blocks to all, logged in or not logged in users
  • Added support for syntax highlighting
  • Added support for shortcodes
  • Added classes to block wrapping divs
  • Few bugs fixed


  • Fixed bug: missing echo for PHP function call example


  • Added option for no code wrapping with div
  • Added option to insert block codes from PHP code
  • Changed HTML codes to disable display on specific pages
  • Selected code block position is preserved after settings are saved
  • Manual insertion can be enabled or disabled regardless of primary display setting
  • Fixed bug: in some cases Before Title display setting inserted code into RSS feed


  • Added option to insert ads also before or after the excerpt
  • Fixed bug: in some cases many errors reported after activating the plugin
  • Few minor bugs fixed
  • Few minor cosmetic changes


  • Fixed blank settings page caused by incompatibility with some themes or plugins


  • Added option to insert ads also on pages
  • Added option to process PHP code
  • Few bugs fixed


  • Number of ad slots increased to 16
  • New tabbed admin interface
  • Ads can be manually inserted also with {adinserter AD_NUMBER} tag
  • Fixed bug: only the last ad block set to Before Title was displayed
  • Few other minor bugs fixed
  • Few cosmetic changes


  • Fixed problem: || in ad code (e.g. asynchronous code for AdSense) causes only part of the code to be inserted (|| to rotate ads is replaced with |rotate|)


  • Fixed bug: manual tags in posts lists were not removed
  • Added position Before title
  • Added support for minimum number of paragraphs
  • Added support for page display options for Widget and Before title positions
  • Alignment now works for all display positions


  • Fixed bug for {search_query}: When the tag is empty {smart_tag} is used in all cases
  • Few changes in the settings page


  • Fixed error with multisite/network installations


  • Fixed bug in Float Right setting display


  • Added option to manually display individual ads
  • Added new ad alignments: left, center, right
  • Added {search_query} tag
  • Added support for category black list and white list


  • HTML entities for {title} and {short_title} are now decoded
  • Added {tag} to display the first tag


  • Fixed bug with rotating ads


  • Added support for rotating ads


  • Added support for different sidebar implementations


  • Initial release

Contributors & Developers

This is open source software. The following people have contributed to this plugin.

Browse the code