{"id":4259,"date":"2023-12-24T17:21:39","date_gmt":"2023-12-24T10:21:39","guid":{"rendered":"https:\/\/dev.artru.net\/?p=4259"},"modified":"2023-12-24T20:16:34","modified_gmt":"2023-12-24T13:16:34","slug":"code-chen-quang-cao-adsense-thu-cong-trong-noi-dung-bai-viet","status":"publish","type":"post","link":"https:\/\/artru.net\/en\/code-chen-quang-cao-adsense-thu-cong-trong-noi-dung-bai-viet\/","title":{"rendered":"Code to manually insert AdSense ads in article content"},"content":{"rendered":"<p class=\"wp-block-paragraph\">Although AdSense supports inserting ads in article content, it doesn&#039;t work very well. Website layouts are often broken due to random ads being inserted. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Currently I am using this PHP code in Oxygen Builder to load AdSense ads every 7 lines, specifically 7 tags. <code data-no-translation=\"\" data-no-auto-translation=\"\">&lt;p><\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1: Turn off automatic ads.<\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"355\" height=\"855\" src=\"https:\/\/cdn.artru.eu.org\/wp-content\/uploads\/2023\/12\/Tat-quang-cao-tu-dong-AdSense.png\" alt=\"Turn off AdSense automatic ads\" class=\"wp-image-4267\"\/><figcaption class=\"wp-element-caption\">Turn off AdSense automatic ads<\/figcaption><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 2: Create In-article Ad unit ads.<\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1280\" height=\"863\" src=\"https:\/\/cdn.artru.eu.org\/wp-content\/uploads\/2023\/12\/Tao-quang-cao-In-article-AdSense.png\" alt=\"Create In-article AdSense ads\" class=\"wp-image-4270\"\/><figcaption class=\"wp-element-caption\">Create In-article AdSense ads<\/figcaption><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 3: Add javascript code to the footer section.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the 4th line: <code data-no-translation=\"\" data-no-auto-translation=\"\">var paragraphs = document.querySelectorAll('.wp-content p');<\/code> please change <code data-no-translation=\"\" data-no-auto-translation=\"\">.wp-content<\/code> equal to the class of the article content element. Each theme is different so you need to check with Devtool (F12).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Nh\u1edb \u0111\u1ed5i &#8220;<code data-no-translation=\"\" data-no-auto-translation=\"\">xxxxxx<\/code>&quot; into your own code based on the script you just created in step 2.<\/p>\n\n\n\n<pre class=\"wp-block-code\" data-no-translation=\"\" data-no-auto-translation=\"\"><code data-no-translation=\"\" data-no-auto-translation=\"\">&lt;script>\ndocument.addEventListener(\"DOMContentLoaded\", () => {\n    var count = 0;\n    var paragraphs = document.querySelectorAll('.wp-content p');\n    for (var i = 0; i &lt; paragraphs.length; i++) {\n        count++;\n    if (count % 7 === 0) {\n            var ad_in_content = document.createElement('ins');\n    ad_in_content.setAttribute('class', 'adsbygoogle');\n    ad_in_content.setAttribute('style', 'display:block; text-align:center;');\n    ad_in_content.setAttribute('data-ad-layout', 'in-article');\n    ad_in_content.setAttribute('data-ad-format', 'fluid');\n    ad_in_content.setAttribute('data-ad-client', 'ca-pub-xxxxxx');\n    ad_in_content.setAttribute('data-ad-slot', 'xxxxxx');\n    paragraphs&#91;i].insertAdjacentElement('afterend', ad_in_content);\n        }\n    }\n\n    const ad_script = document.createElement(\"script\");\n    ad_script.src = \"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-xxxxxx\";\n    ad_script.setAttribute(\"crossOrigin\", \"anonymous\");\n    document.body.appendChild(ad_script);\n    ad_script.addEventListener(\"load\", function () {\n        document.querySelectorAll(\".adsbygoogle\").forEach(function (ad_script) {\n            (window.adsbygoogle = window.adsbygoogle || &#91;]).push({});\n        });\n    });\n});\n&lt;\/script><\/code><\/pre>","protected":false},"excerpt":{"rendered":"<p>M\u1eb7c d\u00f9 AdSense c\u00f3 h\u1ed7 tr\u1ee3 ch\u00e8n qu\u1ea3ng c\u00e1o trong n\u1ed9i dung b\u00e0i vi\u1ebft, nh\u01b0ng n\u00f3 ho\u1ea1t \u0111\u1ed9ng kh\u00f4ng \u0111\u01b0\u1ee3c ho\u00e0n h\u1ea3o cho l\u1eafm. R\u1ea5t hay b\u1ecb v\u1ee1 layout website do b\u1ecb ch\u00e8n qu\u1ea3ng c\u00e1o lung tung. Hi\u1ec7n t\u1ea1i m\u00ecnh \u0111ang s\u1eed d\u1ee5ng m\u00e3 PHP n\u00e0y trong Oxygen Builder \u0111\u1ec3 load qu\u1ea3ng c\u00e1o AdSense sau [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[112],"class_list":["post-4259","post","type-post","status-publish","format-standard","hentry","category-website","tag-adsense"],"_links":{"self":[{"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/posts\/4259","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/comments?post=4259"}],"version-history":[{"count":0,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/posts\/4259\/revisions"}],"wp:attachment":[{"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/media?parent=4259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/categories?post=4259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/tags?post=4259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}