Using WordPress ‘noindex()’ PHP function

The noindex() WordPress PHP function displays a noindex meta tag if required by the blog configuration.

Usage

To use the noindex() function, simply add it as a callback to the ‘wp_head’ action:

add_action('wp_head', 'noindex');

Parameters

The noindex() function has no parameters.

More information

See WordPress Developer Resources: noindex

This function is commonly used in conjunction with the wp_no_robots() function.

Examples

Adding noindex to a private blog

This example demonstrates how to add the noindex() function to the ‘wp_head’ action to prevent indexing of a private blog.

// In functions.php
function my_theme_noindex() {
    if (get_option('blog_public') == 0) {
        add_action('wp_head', 'noindex');
    }
}
add_action('init', 'my_theme_noindex');

Conditionally adding noindex

This example shows how to conditionally add the noindex() function based on the current page type.

// In functions.php
function my_theme_conditional_noindex() {
    if (is_search()) {
        add_action('wp_head', 'noindex');
    }
}
add_action('init', 'my_theme_conditional_noindex');

Using noindex on custom post types

This example demonstrates how to use the noindex() function on specific custom post types.

// In functions.php
function my_theme_noindex_custom_post_type() {
    if (is_singular('my_custom_post_type')) {
        add_action('wp_head', 'noindex');
    }
}
add_action('init', 'my_theme_noindex_custom_post_type');

Adding noindex on archive pages

This example shows how to add the noindex() function to archive pages.

// In functions.php
function my_theme_noindex_archives() {
    if (is_archive()) {
        add_action('wp_head', 'noindex');
    }
}
add_action('init', 'my_theme_noindex_archives');

Adding noindex on specific template

This example demonstrates how to use the noindex() function on a specific template.

// In functions.php
function my_theme_noindex_specific_template() {
    if (is_page_template('template-noindex.php')) {
        add_action('wp_head', 'noindex');
    }
}
add_action('init', 'my_theme_noindex_specific_template');