{"id":588,"date":"2017-11-06T18:49:32","date_gmt":"2017-11-06T18:49:32","guid":{"rendered":"http:\/\/www.netexl.com\/blog\/?p=588"},"modified":"2026-04-02T10:12:28","modified_gmt":"2026-04-02T10:12:28","slug":"how-to-embed-google-adsense-code-inside-a-wordpress-post-without-any-plugin","status":"publish","type":"post","link":"https:\/\/www.netexl.com\/blog\/how-to-embed-google-adsense-code-inside-a-wordpress-post-without-any-plugin\/","title":{"rendered":"How to Embed Google AdSense Code inside a WordPress Post without Any Plugin"},"content":{"rendered":"<p>WordPress does not let you embed\u00a0PHP code inside the posts but uses a feature to create Shortcode functions which are executed whenever they are encountered within a post. This is pretty handy feature since this gives you capability to add some PHP code in your WordPress files and then call it from your posts. The PHP code can be added to Theme&#8217;s functions.php file. We can use this to load ads from within the posts.<\/p>\n<p>Go to WordPress administration -&gt; Appearance -&gt;\u00a0Editor -&gt;\u00a0By default style.css is opened for editing.<\/p>\n<p>Open functions.php by clicking on the file name on right hand side listing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-593 size-full\" src=\"https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/11\/EmbedAdsenseCodeinWordPressPosts.png\" alt=\"Embed Adsense Code in WordPress Posts\" width=\"1348\" height=\"663\" srcset=\"https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/11\/EmbedAdsenseCodeinWordPressPosts.png 1348w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/11\/EmbedAdsenseCodeinWordPressPosts-300x148.png 300w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/11\/EmbedAdsenseCodeinWordPressPosts-768x378.png 768w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/11\/EmbedAdsenseCodeinWordPressPosts-1024x504.png 1024w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/11\/EmbedAdsenseCodeinWordPressPosts-280x138.png 280w\" sizes=\"auto, (max-width: 1348px) 100vw, 1348px\" \/><\/p>\n<p>Now copy the following code\u00a0at the end of the file and change the return string to your Adsense code. Shortcode is defined by calling <strong>add_shortcode<\/strong> function and passing two parameters to it. The first one is\u00a0a given name which will be used in the posts to call this function and second parameter is the name of the function.<\/p>\n<p>You can define multiple functions one for each type of ad and then define a Shortcode for each of them and then call them selectively in your post.<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Adsense code enclosed between single quotes when your Adsense code uses double quotes. \r\n\/\/ If it is using single quotes, then enclose it within double quotes.\r\nfunction getAdSenseCode300x250(){\r\nreturn '&lt;script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"&gt;&lt;\/script&gt;\r\n&lt;ins class=\"adsbygoogle\"\r\n     style=\"display:inline-block;width:300px;height:250px\"\r\n     data-ad-client=\"ca-pub-xxxxxxxxxxxxxxxx\"\r\n     data-ad-slot=\"1111111111\"&gt;&lt;\/ins&gt;\r\n&lt;script&gt;\r\n(adsbygoogle = window.adsbygoogle || []).push({});\r\n&lt;\/script&gt;';\r\n}\r\n\r\nadd_shortcode('ad300x250', 'getAdSenseCode300x250');\r\n<\/pre>\n<p>Click on &#8220;Update File&#8221; to save this. Now we are all set. In order to embed this ad into a post we need to\u00a0use name of the Shortcode within square brackets and put it anywhere in the post.<\/p>\n<p>For ex\u00a0 <strong>[ads300x250]<\/strong> can be added anywhere in the post which will display ad at that location. I have changed the name from the example otherwise it would have simply loaded the ad.<\/p>\n<p>See\u00a0the ad below which is being loaded\u00a0using this method.<\/p>\n<p style=\"text-align: left;\"><div style=\"height:250px;\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\r\n<ins class=\"adsbygoogle\"\r\n     style=\"display:inline-block;width:300px;height:250px\"\r\n     data-ad-client=\"ca-pub-0810934084049672\"\r\n     data-ad-slot=\"8927903874\"><\/ins>\r\n<script>\r\n(adsbygoogle = window.adsbygoogle || []).push({});\r\n<\/script><\/div><\/p>\n<p style=\"text-align: left;\">Another tip; if you want to align this to center in stead of left, simply select the Shortcode and center align it using the rich text editor control. No special code is required to align the ad per your liking.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WordPress does not let you embed\u00a0PHP code inside the posts but uses a feature to create Shortcode functions which are executed whenever they are encountered within a post. This is pretty handy feature since this gives you capability to add some PHP code in your WordPress files and then call it from your posts. The PHP code can be added to Theme&#8217;s functions.php file. We can use this to load ads from within the posts. Go to WordPress administration -&gt; Appearance -&gt;\u00a0Editor -&gt;\u00a0By default style.css is opened for editing. Open functions.php by clicking on the file name on right hand side listing. Now copy the following code\u00a0at the end of the file and change the return string to your Adsense code. Shortcode is defined by calling add_shortcode function and passing two parameters to it. The first one is\u00a0a given name which will be used in the posts to call this[&#8230;]<\/p>\n","protected":false},"author":5,"featured_media":1540,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,14,15],"tags":[],"class_list":["post-588","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-adsense","category-how-to","category-wordpress"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts\/588","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/comments?post=588"}],"version-history":[{"count":4,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts\/588\/revisions"}],"predecessor-version":[{"id":601,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts\/588\/revisions\/601"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/media\/1540"}],"wp:attachment":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/media?parent=588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/categories?post=588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/tags?post=588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}