How to Set Up and Manage the Ads.txt For WordPress

Have you heard of the new ads.txt standard? In this article, we will be going over what ads.txt is and why it is necessary. We will answer the question, what ads.txt means and how you can create and manage ads.txt for your WordPress website.

In the last couple of years, instances of Ad fraud has tripled. To help prevent this the Interactive Advertising Bureau – IAB‎, the governing body responsible for advertisements on the internet, together with top ad companies have developed an approach called Ads.txt which is very similar to the Robots.txt used by web crawlers. Ads.txt protects publishers from ad fraud, and it can potentially increase your ad revenue.

What is Ads.txt File?

Ads.txt is a text file that allows publishers to declare who can sell ads on their website. It is an initiative created by IAB Tech Lab, a non-profit focused on improving digital advertising standards.

The initiative is supported by Google’s advertising platforms like Google Adsense, Doubleclick, and Ad Exchange. It is also supported by many other leading digital advertising platforms.

The primary goal of the ads.txt project is to increase transparency in the programmatic advertising ecosystem. Ads.txt stands for Authorized Digital Sellers and is a simple, flexible and secure method that publishers and distributors can use to publicly declare the companies they authorize to sell their digital inventory.

According to the IAB tech lab, by creating a public record of Authorized Digital Sellers, ads.txt will create greater transparency in the inventory supply chain, and give publishers control over their inventory in the market, making it harder for bad actors to profit from selling counterfeit inventory across the ecosystem.

As publishers adopt ads.txt, buyers will be able to more easily identify the Authorized Digital Sellers for a participating publisher, allowing brands to have confidence they are buying authentic publisher inventory.

Why is Ads.txt file important?

Ad fraud cost millions of dollars in damages to online advertisers and publishers each year. People with malicious intent continuously try to trick ad platforms with domain spoofing, fake clicks, fake impressions, and more.

Ads.txt file improves transparency in the online advertisement by allowing publishers to declare who can sell their ad inventory. Here is how it works:

If you are an advertiser and someone tells you that they can display your ad on a particular website, then you can go and checkout that website’s ads.txt file. It will show you if the company you are dealing with has the permission to sell advertising space on that particular website or not.

Since this data is publicly available, it can be crawled, stored, and searched by advertisers, publishers, and resellers.

If you are displaying ads on your website using Google Adsense, DoubleClick, or Ad Exchange, then adding Ads.txt file is recommended. Many other advertising platform and software also support or require ads.txt file to be declared.

We will be going over 2 different ways to set up ads.txt on your website. As a side note, the ads.txt file is supposed to be in the root directory of your website.

1. How to Set Up Ads.txt Files in Using a Plugin

The first approach we are going to see is to use a plugin. WordPress makes it easy to install plugins and extend the functionality of your website.
To set this up, you need to install and activate the Ads.txt Manager plugin. For more details, see our step by step guide on how to install a WordPress plugin.

Once you have successfully activated the plugin, you need to visit the Settings » Ads.txt page to configure plugin settings.

You can then add your publisher details in the text box provided. One thing to note is that this plugin currently only works for root level domain like example.com. It does not work for WordPress installed in a subdomain, subdirectory, or a single site in a multi-site network.

2. How to Set Up Ads.txt with a code change

This second approach will look at adding some code changes to functions.php which will allow you to enable ads.txt. Paste the code below into the functions.php to allow you to setup your ads.txt.

// TODO: change `my_theme` prefix to my theme's prefix!
/**
 * Register the rewrite rule for /ads.txt request.
 */
function my_theme_adstxt_rewrite() {
    add_rewrite_rule( '^ads\.txt$', 'index.php?my_theme_adstxt=true', 'top' );
}
add_action( 'init', 'my_theme_adstxt_rewrite', 10 );
 
/**
 * Filter the list of public query vars in order to allow the WP::parse_request
 * to register the query variable.
 *
 * @param array $public_query_vars The array of whitelisted query variables.
 *
 * @return array
 */
function my_theme_adstxt_query_var( $public_query_vars ) {
    $public_query_vars[] = 'my_theme_adstxt';
    return $public_query_vars;
}
add_filter( 'query_vars', 'my_theme_adstxt_query_var', 10, 1 );
 
/**
 * Hook the parse_request action and serve the ads.txt when custom query variable is set to 'true'.
 *
 * @param WP $wp Current WordPress environment instance
 */
function my_theme_adstxt_request( $wp ) {
    if ( isset( $wp->query_vars['my_theme_adstxt'] ) && 'true' === $wp->query_vars['my_theme_adstxt'] ) {
        /*
         * Set proper content-type per specification in
         * https://iabtechlab.com/wp-content/uploads/2017/09/IABOpenRTB_Ads.txt_Public_Spec_V1-0-1.pdf :
         *
         * The HTTP Content-type should be ‘text/plain’, and all other Content-types should be treated
         * as an error and the content ignored.
         */
        header( 'Content-Type: text/plain' );
 
        // The code expects an existing ads.txt file in the root of your active theme.
        echo file_get_contents( get_stylesheet_directory() . '/ads.txt' );
        exit;
    }
}
add_action( 'parse_request', 'my_theme_adstxt_request', 10, 1 );

Once you have successfully setup your ads.txt Upon activation, you can validate it with your ad provider.

Troubleshooting Earnings at risk Error in Adsense

A missing declaration in your ads.txt file may result in the following warning message in your Google Adsense account.

Earnings at risk – One or more of your ads.txt files doesn’t contain your AdSense publisher ID. Fix this now to avoid severe impact to your revenue.

This message indicates that your ads.txt file doesn’t have your publisher ID. To fix this message, simply go to Settings » Ads.txt page to make sure that you have correct publisher ID in plugin settings.

We hope this article helped you learn how to create and manage the ads.txt file in WordPress. Feel free to leave your comments on your experience with installing ads.txt.

How to Set Up and Manage the Ads.txt For WordPress
Rate this post

Join over 20,000 users and get our best content

Each week we send 1 email to 20,000 smart entrepreneurs just like you. Enter your email below to join the crew.

Thank you for subscribing.

Something went wrong.