Can someone please advise how to call adsense script in a certain place of the body of the page? For example, after a specific class or tag (after title tag) ?
This is the function I'm using in child themes function.php file.
function adsense_script() {
$path = $_SERVER['REQUEST_URI'];
$find = 'sdm';
$pos = strpos($path, $find);
if ($pos !== false) {
<Script>
//adsense script
</Script>
<?php
}
}
I tried following, but it does not display the ads properly.
add_action( 'wp_body_open', 'adsense_script' );
If you want the AdSense script to appear after a particular class or tag then you will probably need to use JavaScript to inject the AdSense script, which could be complicated.
A simpler solution would be add a custom shortcode something like:
add_shortcode('render-adsense', 'render_adsense');
function render_adsense() {
// your function here including the adsense script
}
This way you can manually add the AdSense script wherever you like on the page via the normal page editor.
You can also inject it at a certain point in your template by using the do_shortcode function in a PHP code block.
Documentation on shortcodes: https://codex.wordpress.org/Shortcode_API