Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have wordpress site where i have few custom type pages. I load the content of those pages with jquery load. But I dont want that those pages are directly visible. So if user goes to http://www.domain.com/work/project-name it wouldnt be visible. Or atleast that search bots wouldnt index these pages that are under /work/

I use single.php for the html for the page. What could I do? Just deny search bots to go with robots.txt? Could I somehow deny users to go there too but still keep that jquery load works normally.

Thanks!

share|improve this question
add comment

1 Answer 1

Add a simple checkbox to the post editor to toggle the generation of a meta field that disallows search engines to index your content. Then hook into wp_head, check for the setting and print that field.

Sample code for a plugin:

add_action( 'post_submitbox_misc_actions', 'show_noindex_checkbox' );
add_action( 'save_post', 'save_noindex', 10, 2 );
add_action( 'wp_head', 'insert_noindex_meta' );

function show_noindex_checkbox()
{
    $post_id = get_the_ID();

    $name    = 'noindex';
    $id      = $name . '_id';
    $value   = esc_attr( get_post_meta( $post_id, '_noindex', TRUE ) );
    $checked = checked( $value, 1, FALSE );
    $label   = 'Disallow search engine indexing';
    $nonce   = wp_nonce_field( '_noindex', '_noindex_nonce', TRUE, FALSE );

    print <<<EOD
<div class="misc-pub-section">
    <label for="$id">
        <input type="checkbox" $checked id="$id" name="$name" value="1" />
        $nonce
        $label
    </label>
</div>
EOD;
}

function save_noindex( $post_id, $post )
{
    if ( wp_is_post_autosave( $post ) )
        return;

    if ( ! current_user_can( 'edit_post', $post_id ) )
        return;

    if ( ! isset ( $_POST[ '_noindex_nonce' ] ) )
        return;

    if ( ! wp_verify_nonce( $_POST[ '_noindex_nonce' ], '_noindex' ) )
        return;

    if ( ! isset ( $_POST[ 'noindex' ] ) )
        return delete_post_meta( $post_id, '_noindex' );

    if ( 1 != $_POST[ 'noindex' ] )
        return;

    update_post_meta( $post_id, '_noindex', 1 );
}

function insert_noindex_meta()
{
    if ( is_singular() and '1' !== get_post_meta( get_the_ID(), '_noindex', TRUE ) )
        wp_no_robots();
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.