Home | Wordpress | Wordpress Plugins | SAM 2 and caching plugins

SAM 2 and caching plugins

cacheSince version 2.0.73 plugin Simple Ads Manager supports caching plugins . He really supports ALL caching plugins and it’s great . But there is a small fly in the ointment. This fly is javascript function document.write().

This function can be used only in the process of building DOM (Document Object Model) and it absolutely can not be used after the DOM has already been built. However, when SAM is used in caching plugins compatibility mode, it inserts advertisements into a document using AJAX requests after the browser is already built DOM of document. Inserting function document.write() in the output document at this point, guaranteed kills this document!

How to deal with this problem I want to talk about in this article. If you do not use javascript codes as codes of advertisements, then you have nothing to worry about. Otherwise, you should read this article.

The only true way to combat this scourge, is the removal of function document.write() from code of the ad. For this purpose it is necessary to inspect an ad code regarding this function. For example, survey the code of Yandex Direct ads:

What to do? Just replace the document.write() with what they wanted to write to the document during its creation:

It’s very simple … If the function is defined clearly… And if not?

If not, create a test HTML document and paste the code into it. For example (Text Links Ads):

Open the document code, it is possible, in any browser, and see what came of it:

This code is not difficult to transform to data for image mode of plugin:

SAM Image Mode

That’s it. If you always will follow these instructions, you will not have problems with Simple Ads Manager caching plugins compatibility mode.

Good luck!

© 2014, minimus. All rights reserved.

About minimus

Senior Full Stack Web-developer (React, Node.js). Also: Development using Object Pascal, C#, SQL, PHP, Javascript.

Check Also

XAds for SAM Pro

This addon provides extended visualization of ads served by SAM Pro (Lite and Free edition). …


  1. Since the update I have the problem, that with active SAM i cannot choose the image sizes of the images which i will to insert into an article. With deactivation of SAM everything works fine.
    Total Comments by Jerry: 2

  2. Hi,
    I tried to install and make SAM run on my site. If I let it run in PHP mode, all is fine… but since I have a caching plugin, I need to let it run in js mode. Here I have trouble: I get the error 403 forbidden all the time and the widget does not display. 
    The console log I have, is the following:

    POST http://www.diariodavancouver.com/wordpress/wp-content/plugins/simple-ads-manager/sam-ajax-loader.php 403 (Forbidden) jquery.js?ver=1.10.2:5



    (anonymous function)sam-layout.min.js?ver=2.2.80:1



    (anonymous function)sam-layout.min.js?ver=2.2.80:1





    I’m using WP super cache and BWP minify, but I get the same error even though I disable them both. The only way I can make it work is with SAM in PHP mode but because of the caching it’s not working properly (always displaying the same ad..)
    Any suggestion? Thank you!
    Total Comments by Diego: 8

    • 403 – Access is denied. Check access permissions to the folder and files of plugin. Should be 755 or 644.

      • Thanks for the answer! Sorry I forgot to mention that I checked the plugin’s permissions and all folders and files are 755 or 644.
        Total Comments by Diego: 8

        • Read here and try to resolve this problem using all methods from article.

          • Thanks again for your time,
            I tried again following your instructions and the document you suggested me. I’ve also setup a local version of wordpress and run it locally. On the local instance I run WP version 3.8 and the latest version of your plugin.  In this case it does not give me the 403 error but it just does not display the ad images. Any more suggestions? What else could I do to make it work in caching compatible mode?
            Thank you
            Total Comments by Diego: 8

          • Now I set all the permissions for the plugins’ files to be 755. If I access the file sam-ajax-loader.php directly from the URL bar, it gives me back the value -1 (if I set the permissions as 777 it says forbidden). But on my site’s page I still have a 403 message in my console… 
            Therefore the GET request passes through while the POST request does not… Is it possible that the permissions filter on 2 different levels the requests? But if so, why other plugins I have do not have these issues?

            Just trying to tickle my brain to come to a solution..
            Total Comments by Diego: 8

          • So the problem seems to be the POST request… do you know if it can be turned into a GET in the code? 
            Total Comments by Diego: 8

            • YAY! Solution found, it was something on the .htaccess file that had to be modified to allow POST requests on PHP modules. This code:

              The service provider is Netsons.
              Thank you for your patience and help!
              Total Comments by Diego: 8

      • …and it does not work 🙁
        Total Comments by Diego: 8

  3. Hi minimus,
    thanks for your great work! How about dynamically loading AdSense codes on cached pages like when I have 2 banners I want to test?
    Total Comment by Thomas: 1

  4. Hi!
    I am using W3 Total Cache and SAM – but sadly those two deliver the same ad for all 4 adsblock I have defined – and not one of the 12 others mixed into it ^^
    Oh, nevermind – it works now. It seems the two have a problem with each other if I freshly empty the cache.
    Total Comments by Franz Sauerstein: 2

  5. Ребята, на WP 3.9 не пашет кнопочка в визуальном редакторе, можете подправить , выпустить обновление?
    Так люблю ваш плагин!
    Total Comment by lorlus: 1

  6. Since I updated to WP 3.9, I have lost the pulldown on the “New Post” page. I can’t simply select which AdPlace to use. I now have to copy and paste from an older post. What am I doing wrong? Thanks for this excellent plugin! -Steve
    Total Comment by Steve: 1

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.