# SEO Title, Description and Noindex Fields

> Adds a small SEO box to the editor of posts and pages with a custom title, a meta description and a "hide from search engines" option, for sites without an SEO plugin.

- Type: PHP
- Runs: Run everywhere
- Category: [SEO](https://wpide.com/library/category/seo/)
- Tags: meta-description, title, noindex
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/simple-seo-fields/

## Code

```php
$wpide_seo_types = function () {
    return array_values( array_diff( get_post_types( array( 'public' => true ) ), array( 'attachment' ) ) );
};

add_action( 'add_meta_boxes', function () use ( $wpide_seo_types ) {
    add_meta_box( 'wpide-seo', 'SEO', function ( $post ) {
        wp_nonce_field( 'wpide_seo_save', 'wpide_seo_nonce' );
        $title       = get_post_meta( $post->ID, '_wpide_seo_title', true );
        $description = get_post_meta( $post->ID, '_wpide_seo_description', true );
        $noindex     = get_post_meta( $post->ID, '_wpide_seo_noindex', true );
        echo '<p><label for="wpide-seo-title"><strong>Title</strong></label><br /><input type="text" class="widefat" id="wpide-seo-title" name="wpide_seo_title" value="' . esc_attr( $title ) . '" maxlength="120" /></p>';
        echo '<p><label for="wpide-seo-description"><strong>Meta description</strong></label><br /><textarea class="widefat" rows="3" id="wpide-seo-description" name="wpide_seo_description" maxlength="320">' . esc_textarea( $description ) . '</textarea><span class="description">About 150 characters works best.</span></p>';
        echo '<p><label><input type="checkbox" name="wpide_seo_noindex" value="1"' . checked( $noindex, '1', false ) . ' /> Hide from search engines (noindex)</label></p>';
    }, $wpide_seo_types(), 'normal', 'low' );
} );

add_action( 'save_post', function ( $post_id ) {
    if ( ! isset( $_POST['wpide_seo_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wpide_seo_nonce'] ) ), 'wpide_seo_save' ) ) {
        return;
    }

    if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || wp_is_post_revision( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }

    $fields = array(
        '_wpide_seo_title'       => isset( $_POST['wpide_seo_title'] ) ? sanitize_text_field( wp_unslash( $_POST['wpide_seo_title'] ) ) : '',
        '_wpide_seo_description' => isset( $_POST['wpide_seo_description'] ) ? sanitize_textarea_field( wp_unslash( $_POST['wpide_seo_description'] ) ) : '',
        '_wpide_seo_noindex'     => empty( $_POST['wpide_seo_noindex'] ) ? '' : '1',
    );

    foreach ( $fields as $key => $value ) {
        if ( '' === $value ) {
            delete_post_meta( $post_id, $key );
        } else {
            update_post_meta( $post_id, $key, $value );
        }
    }
} );

add_filter( 'pre_get_document_title', function ( $title ) {
    if ( is_singular() ) {
        $custom = get_post_meta( get_queried_object_id(), '_wpide_seo_title', true );

        if ( '' !== $custom ) {
            return $custom;
        }
    }

    return $title;
}, 20 );

add_action( 'wp_head', function () {
    if ( ! is_singular() ) {
        return;
    }

    $description = get_post_meta( get_queried_object_id(), '_wpide_seo_description', true );

    if ( '' !== $description ) {
        echo '<meta name="description" content="' . esc_attr( $description ) . '" />' . "\n";
    }
}, 1 );

add_filter( 'wp_robots', function ( $robots ) {
    if ( is_singular() && get_post_meta( get_queried_object_id(), '_wpide_seo_noindex', true ) ) {
        $robots['noindex'] = true;
        $robots['follow']  = true;
    }

    return $robots;
} );

add_filter( 'wp_sitemaps_posts_query_args', function ( $args ) {
    $args['meta_query'] = array(
        array(
            'key'     => '_wpide_seo_noindex',
            'compare' => 'NOT EXISTS',
        ),
    );

    return $args;
} );
```

## Notes

Do not combine it with an SEO plugin or with the Automatic Meta Description snippet, or pages will get two descriptions. Noindexed entries are also left out of the XML sitemap.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "SEO Title, Description and Noindex Fields" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
