---
title: "How to add the X-Robots-Tag header to Rank Math SEO?"
description: "Only today a customer asked us for help in adding the X-Robots-Tag HTTP header for all that content which had previously been marked as noindex in the WordPress plugin Rank Math SEO – AI SEO Tools to Dominate SEO Rankings."
url: https://artprojectgroup.com/how-to-add-the-x-robots-tag-header-to-rank-math-seo
date: 2025-04-23
modified: 2025-04-25
author: "Art Project Group"
image: https://artprojectgroup.com/wp-content/uploads/2023/02/programacion-web-blog.webp
categories: ["Tutorials"]
tags: ["functions.php", "PHP", "Rank Math SEO", "SEO", "Web programming", "WordPress", "WordPress plugins", "Yoast SEO"]
type: post
lang: en-US
---

# How to add the X-Robots-Tag header to Rank Math SEO?

Only today a customer asked us for help in adding the `X-Robots-Tag` HTTP header for all that content which had previously been marked as `noindex` in the WordPress plugin **Rank Math SEO – AI SEO Tools to Dominate SEO Rankings**.

Although we understand that this is something the Rank Math SEO plugin itself should handle directly from its source code, it turns out that this is not the case.

While Rank Math SEO adds the meta tag `<meta name="robots" content="nofollow, noindex">` to all content marked as `noindex` in its settings or directly in its metabox, it does not do the same with the `X-Robots-Tag` HTTP header, and we understand that this is not right, since for them it is really simple to add it.

We have referred the customer to Rank Math SEO’s technical support so that they add this feature to their code, but while they do or do not add it, we are sharing with you the solution we have created and that we have asked them to add.

### Solution

The PHP code we propose has to be added to the *functions.php* file of the child theme, and it has to be done on its first line, since otherwise it will give you problems. Specifically, this is the function that has to be added:

Copy to clipboardSyntax Highlighteruse RankMath\Paper\Paper;

//Adds the X-Robots-Tag header according to what is set in Rank Math
function apg_add_x_robots_tag() {
if ( class_exists( '\RankMath\Paper\Paper' ) ) {
$robots = Paper::get()->get_robots();

if ( isset( $robots[ 'index' ] ) && 'noindex' === $robots[ 'index' ] ) {
header( 'X-Robots-Tag: noindex, nofollow', true );
}
}
}
add_action( 'send_headers', 'apg_add_x_robots_tag' );Line 1 is the one that will give you problems if it is not added at the beginning of the *functions.php* file; the `apg_add_x_robots_tag` function itself you can add wherever you like within that same file, and it will not cause you any problem.

The way the function works is very simple: it obtains information from the Rank Math SEO plugin’s settings and, depending on it, adds the header or not, so that it only adds it if the content has been marked as `noindex`, which is what we are interested in adding.

### How can I see whether it works or not?

You can do it directly from your favourite web browser with the Web Inspector tool, reviewing the document headers where this new header will or will not appear, or by installing an extension such as [Robots Exclusion Checker](https://chromewebstore.google.com/detail/robots-exclusion-checker/lnadekhdikcpjfnlhnbingbkhkfkddkl), which shows you visually the status of the page you are viewing in the *robots.txt* file, the `robots` meta tag, the `X-Robots-Tag` header and the `Canonical` tag.

### Conclusion

We hope you find this code useful; it is easily replicable for other WordPress plugins such as **Yoast SEO**, with which you would have to play with the `WPSEO_Meta::get_value()` function to obtain a result identical to the one proposed. And if you need help, you know you have our [Technical support](/shop/technical-support) to help you.
