网站文章老是被别人复制怎么办?万万没想到我这个小网站也有人采集和复制。上网找了一下,其实从根本避免可能性比较低,用这两段小代码可以为那些人增加一点难度。
网站找了一段说关闭Feed功能可以防止被采集。但是事实上只是想想而以!
不多说,先给停用这两项再说。在主题文件中找到functions.php然后再加入下面这一段代码。
//免插件实现WordPress复制文章内容自动添加版权信息
function copyright() { ?>
<script type=’text/javascript’>
function addLink() {
var body_element = document.getElementsByTagName(‘body’)[0];
var selection;
selection = window.getSelection();
var pagelink = “<br /><br /> 云东方: <?php if(is_single()){ the_title();}?> 来源地址:<a href='”+document.location.href+”‘>”+document.location.href+”</a>”;
var copy_text = selection + pagelink;
var new_div = document.createElement(‘div’);
new_div.style.left=’-99999px’;
new_div.style.position=’absolute’;
body_element.appendChild(new_div );
new_div.innerHTML = copy_text ;
selection.selectAllChildren(new_div );
window.setTimeout(function() {
body_element.removeChild(new_div );
},0);
}
document.oncopy = addLink;
</script>
<?php
}
add_action( ‘wp_head’, ‘copyright’);
//禁用 feed
function disable_our_feeds() {
wp_die( __(‘Error: No RSS Feed Available, Please visit our homepage.’));
}
add_action(‘do_feed’, ‘disable_our_feeds’, 1);
add_action(‘do_feed_rdf’, ‘disable_our_feeds’, 1);
add_action(‘do_feed_rss’, ‘disable_our_feeds’, 1);
add_action(‘do_feed_rss2’, ‘disable_our_feeds’, 1);
add_action(‘do_feed_atom’, ‘disable_our_feeds’, 1);
我用颜色区分了这两段代码。不过有些朋友说加了没什么用。
或许你的主题中的functions.php文件把functions.php功能指向了另一个文件。
如果看到是这样的:
<?php
// include xiu theme functions file
include ‘functions.xiu.php’;
// custom functions
你就需要到functions.xiu.php这个文件中去加代码。
//上传图片自动添加Alt和图像描述
add_action( ‘add_attachment’, ‘my_set_image_meta_upon_image_upload’ );
function my_set_image_meta_upon_image_upload( $post_ID ) {
// Check if uploaded file is an image, else do nothing
if ( wp_attachment_is_image( $post_ID ) ) {
$my_image_title = get_post( $post_ID )->post_title;
// Sanitize the title: remove hyphens, underscores & extra spaces:
$my_image_title = preg_replace( ‘%\s*[-_\s]+\s*%’, ‘ ‘, $my_image_title );
// Sanitize the title: capitalize first letter of every word (other letters lower case):
$my_image_title = ucwords( strtolower( $my_image_title ) );
// Create an array with the image meta (Title, Caption, Description) to be updated
// Note: comment out the Excerpt/Caption or Content/Description lines if not needed
$my_image_meta = array(
‘ID’ => $post_ID, // Specify the image (ID) to be updated
‘post_title’ => $my_image_title, // Set image Title to sanitized title
‘post_excerpt’ => $my_image_title, // Set image Caption (Excerpt) to sanitized title
‘post_content’ => $my_image_title, // Set image Description (Content) to sanitized title
);
// Set the image Alt-Text
update_post_meta( $post_ID, ‘_wp_attachment_image_alt’, $my_image_title );
// Set the image meta (e.g. Title, Excerpt, Content)
wp_update_post( $my_image_meta );
}
}
原创文章,作者:校长,如若转载,请注明出处:https://www.yundongfang.com/Yun20044.html