WordPress sets the article reading permission (open to search engines)

Source: Internet
Author: User
Tags php file

Implementation code

If page caching is not enabled for your WordPress site, this requirement is not hard to achieve, we can refer to the WordPress previously written seo/seo.html "target =" _ blank "> The search engine hides the code in a specific article and makes some modifications. Add the following php code to the functions. php file of the current topic and use UTF8 to save the code:

The code is as follows: Copy code
// Add option on the article/page editing page
Function ludouseo_add_custom_box (){
Add_meta_box ('ludou _ se_only ', 'search engine exclusive', 'ludou _ se_only ', 'post', 'side', 'Low ');
Add_meta_box ('ludou _ se_only ', 'search engine exclusive', 'ludou _ se_only ', 'page', 'side', 'Low ');
}
Add_action ('add _ meta_boxes ', 'ludouseo _ add_custom_box ');
Function ludou_se_only (){
Global $ post;
 
// Add verification fields
Wp_nonce_field ('ludou _ se_only ', 'ludou _ se_only_nonce ');
 
$ Meta_value = get_post_meta ($ post-> ID, 'Dou _ se_only ', true );
If ($ meta_value)
Echo '<input name = "ludou-se-only" type = "checkbox" checked = "checked" value = "1"/> allows only search engines to view ';
Else
Echo '<input name = "ludou-se-only" type = "checkbox" value = "1"/> allows only search engines to view ';
}
// Save option settings
Function ludouseo_save_postdata ($ post_id ){
// Verify
If (! Isset ($ _ POST ['ludou _ se_only_nonce '])
Return $ post_id;
$ Nonce = $ _ POST ['ludou _ se_only_nonce '];
 
// Verify that the field is valid
If (! Wp_verify_nonce ($ nonce, 'Dou _ se_only '))
Return $ post_id;
  
// Determine whether to save automatically
If (defined ('Doing _ AUTOSAVE ') & DOING_AUTOSAVE)
Return $ post_id;
    
// Verify user permissions
If ('page' = =$ _ POST ['post _ type']) {
If (! Current_user_can ('edit _ page', $ post_id ))
Return $ post_id;
  }
Else {
If (! Current_user_can ('edit _ post', $ post_id ))
Return $ post_id;
  }
 
// Update settings
If (! Empty ($ _ POST ['ludou-se-only'])
Update_post_meta ($ post_id, 'ludou _ se_only ', '1 ');
Else
Delete_post_meta ($ post_id, 'ludou _ se_only ');
}
Add_action ('SAVE _ post', 'ludouseo _ save_postdata ');
Function do_ludou_se_only (){
// This function is only valid for articles and pages
If (is_singular ()){
Global $ post;
$ Is_robots = 0;
$ Ludou_se_only = get_post_meta ($ post-> ID, 'ludou _ se_only ', true );
  
If (! Empty ($ ludou_se_only )){
// The following is the search engine Agent keyword array
// It's a little simple. Please optimize it yourself.
$ Bots = array (
'Baidider ',
'Googlebot ',
'Sogou spider ',
'Sogou web spider ',
'Yodaobot ',
'Bingbot ',
'360spider ',
'Slup ',
'Msnbot ',
'Yahoo-blogs ',
'Psbot ',
'Yandex ',
'Www. 111cn. Net ',
'Blogsearch ',
'Easouspider ',
'Mediapartners-Google'
);
  
$ Useragent = $ _ SERVER ['http _ USER_AGENT '];
  
If (! Empty ($ useragent )){
Foreach ($ bots as $ lookfor ){
If (stristr ($ useragent, $ lookfor )! = False ){
$ Is_robots = 1;
Break;
          }
        }
      }
    
// If it is not a search engine, an error message is displayed.
// Logged-on users are not affected
If (! $ Is_robots &&! Is_user_logged_in ()){
Wp_die ('you are not authorized to view this article! ');
      }
    }
  }
}
Add_action ('WP ', 'Do _ ludou_se_only ');

Usage

After successfully adding the above code to functions. php of the current topic, we can use it normally. It is completely silly. On the WordPress background article and page editing page, we can see the following selection box at the bottom of the right sidebar:

 

If you want to disable search engine crawling on the current article/page, select this option. When checked, the following error message is displayed when this article/page is accessed by a common visitor (the search engine and logged-on users are not affected ):

Original article from: ludou.org

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.