Set up the ads.txt file
The ads.txt specification is an IAB-approved text file to prevent unauthorized sales of inventory. More information about ads.txt can be found on IAB’s website: https://iabtechlab.com/ads-txt/
The ads.txt file must be located on a site’s root domain, as well as any subdomains as needed. This can be accomplished via code or a plugin.
Prerequisite
A user must have GitHub access with at least write
capabilities in order to add plugins or custom code to a site’s repository.
Enabling ads.txt with code
On the VIP Platform, the ads.txt file must be committed to a site’s theme directory, and a rewrite rule for the file’s URL must be added.
A rewrite rule for the ads.txt file URL can be added with code similar to this example:
/**
* 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 public 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 );
When rewrite rules are added to a site, especially if the added redirect is not working as expected, the site’s rewrite rules must be flushed.
To flush rewrite rules in the WP Admin:
- Hover over VIP in the WordPress Admin dashboard of the site.
- Select Rewrite Rules from the fly-out menu.
- Select the “Flush Rules” button at the upper right of the Rewrite Rules dashboard.
Enabling ads.txt with a plugin
The Ads.txt Manager plugin allows ads.txt
for a site to be created, modified, and validated from within the WP Admin dashboard. This method saves the file content and settings to the site’s database. Requests made to /ads.txt
via $_SERVER['REQUEST_URI']
serve the saved contents as if serving an actual text file.