Wordpress gotchas (the loop, elementor)

There are several Wordpress-specific issues to watch out for.

The Loop and setTargeting()

Most Wordpress themes heavily use a feature called The Loop . Incorrect or buggy use of this feature frequently causes ad targeting problems.

Many websites use setTargeting() to send metadata to GAM. In PHP/Wordpress, the code can be implemented like this to set the post_id key-value.

htlbid.cmd.push(() => {
  htlbid.setTargeting("post_id", "<?php echo $post->post_id ?>");
});

A problem can occur here with The Loop. When used multiple times on a page, or when used to display “recent posts” or other paginated content after the primary article, the $post variable will refer to the last post, not the primary post. As a result, the wrong post_id may be sent to GAM.

Identify this problem by spot-checking what targeting metadata is sent to GAM by using a tool such as HTL Debug.

Most commonly, this problem occurs on the homepage. If the homepage is sending a post_id to GAM, that is usually incorrect, and in fact the post_id belongs to one of the articles linked on the homepage, not the homepage itself. (Note: some sites DO have a unique ID for the homepage, so the existence of post_id on the homepage is suggestive, but not necessarily indicate a problem with use of The Loop)

Elementor and Theme-specific problems

Certain Wordpress themes contain built-in CSS rules that cause problems for ad rendering. One known problem is the Elementor Theme, which contains this CSS rule:

.elementor iframe {
  max-width: 100%;
}

This CSS rule interferes with the width/height of iframes. GAM normally sets these values to match the size of the ad creative. If you notice stretched ads, especially on Wordpress sites, this CSS rule is often to blame.

In many cases, the rule can removed, or modified to be more specific, so that it only applies to certain iframes (often, video embeds)