Benjoe
Benjoe

Reputation: 466

Programatically prevent search engines to crawl your WordPress Posts

I want to programatically stop search engines to crawl a WordPress posts using PHP.

The scenario is, I'm creating a dummy posts along with user registration, I want it to be not searchable otherwise the user edit and save it already. Is there any way to do that?

Upvotes: 2

Views: 271

Answers (3)

Sarathlal N
Sarathlal N

Reputation: 899

Use below code snippet in your theme's functions.php file or custom plugin file.

function ar456_header_metadata() {

    // Seperate Conditional Tag for Pages & single page in other post type

    if( is_page( array( 'page-slug', 'post_id' ) ) or is_single( array( 'page-slug', 'post_id' ) ) ) {  ?>

    <meta name="robots" content="noindex,follow">

    <?php   }

}
add_action( 'wp_head', 'ar456_header_metadata' );

In the code, You can see two conditional tag is_page() and is_single(). The is_page() is for Page post type and is_single() for all other post type.

Update your Page ID, title or slug in conditional tags & meta tag will be available in these pages.

Example given below.

if( is_page( array( 'sample-page', '3' ) ) or is_single( array( 'hello-world', '6' ) ) ) {  ?>

https://developer.wordpress.org/reference/functions/is_page/ https://developer.wordpress.org/reference/functions/is_single/

Upvotes: 2

Edunikki
Edunikki

Reputation: 237

Ceejayoz has the perfect answer, but I don't have the reputation to comment directly. You will have to create a template that specifically blocks the robot and then call that template for the posts and pages you want omitting.

Upvotes: 0

ceejayoz
ceejayoz

Reputation: 180004

If you want all posts to be excluded, just add the following to your theme's <head> block:

<meta name="robots" content="noindex">

If you want some posts to be excluded, you'll have to include that meta tag conditionally according to whatever rules you want in place.

Upvotes: 1

Related Questions