程序员的世界

WordPress 代码实现文章自动添加原创或转载版权声明

代码修改

以下功能在WordPress 最新版本:4.9.2上测试通过。

以下代码修改可以直接通过FTP修改文件,也可以通过后台中“外观->编辑”功能直接修改。

PHP函数

此段代码放在主题目录下的 functions.php 文件中

/**
 * 发布文章/页面时自动添加默认的自定义字段
 * copyright_type 0 不添加任何声明,1 原创,2 转载,3 翻译
 * copyright_reprint_title 转载或翻译的原文标题
 * copyright_reprint_url 转载或翻译的原文链接
 */
function add_copyright_field($post_ID) {
    global $wpdb;
    if(!wp_is_post_revision($post_ID)) {
                // 默认启用“原创”版权声明
        add_post_meta($post_ID, 'copyright_type', '1', true);
        add_post_meta($post_ID, 'copyright_reprint_title', '', true);
        add_post_meta($post_ID, 'copyright_reprint_url', '', true);
    }
}
// 发布页面时启用版权声明
add_filter('publish_page', 'add_copyright_field');
// 发布文章时启用版权声明
add_filter('publish_post', 'add_copyright_field');

/**
 * 发布文章/页面时自动添加版权声明文本
 */
function my_copyright($outer) {
    if(!is_singular()){ return $outer; }

    global $post;
        // 文章或页面属性
    $post_id = $post->ID;
    $post_title = $post->post_title;
    $post_url = get_permalink($post_id);
        // 转载或翻译的文章属性
    $copy_type = get_post_meta($post_id, 'copyright_type', true);
    $copy_title = get_post_meta($post_id, 'copyright_reprint_title', true);
    $copy_url = get_post_meta($post_id, 'copyright_reprint_url', true);
    $copy_text = $copy_title ? $copy_title : $copy_url;
    if(!$copy_type || $copy_type == '0'){ return $outer; }

        // 样式名称 .copyright-text
    $outer .= '<div class="copyright-text">';
        // 声明文本,可自行修改
    if($copy_type == '1'){	
        $outer .= '<p>';
        $outer .= '原创文章,转载请注明:转载自<a href="'. get_settings('home') .'" rel="bookmark">'. get_bloginfo('name') .'</a>';
        $outer .= ' 本文链接地址:<a href="'. $post_url .'" rel="bookmark">'. $post_title .'</a>';
        $outer .= '</p>';
    }else if($copy_type == '2'){
        $outer .= '<p>转载自:<a href="'. $copy_url .'" target="blank" rel="nofllow">'. $copy_text.'</a></p>';
    }else if($copy_type == '3'){
        $outer .= '<p>翻译自:<a href="'. $copy_url .'" target="blank" rel="nofllow">'. $copy_text.'</a></p>';
    }
    $outer .= '</div>';
    return $outer;
}
// 最后的参数“9”表示函数执行顺序,因为我使用了“相关文章”插件,该插件执行顺序为“10”。
// 如果需要把版权声明文本模块写在“相关文章”之前,必须修改执行顺序小于10。
add_filter('the_content', 'my_copyright', 9);

CSS样式

此段代码放在主题目录下的 style.css 文件中

.copyright-text {
    width: 100%;
    height: auto;
    background-color: #f2f2f2;
    box-shadow: 0 0 5px #EEE;
    font-family: 'comic sans ms', fantasy !important;
    margin: 20px auto;
    padding: 10px;
    border-radius: 9px;
    border-collapse: collapse;
    text-align: left;
}

.copyright-text p {
    padding: 0px;
    margin: 0px;
}

使用方法

编辑文章或页面时,在WordPress编辑器的自定义栏目中,使用上面PHP代码添加的 copyright_type copyright_reprint_title copyright_reprint_url 参数,具体参数含义参见PHP代码注释。

如果是原创文章,可以不用填写,文章保存时会自动设置copyright_type字段值为“1”,即原创,copyright_reprint_title copyright_reprint_url字段值为空。

《WordPress 代码实现文章自动添加原创或转载版权声明》

效果展示

效果图中的“相关文章”模块使用插件实现,参见:WordPress 必备插件推荐及使用说明

文章标题栏的“原创”标识使用代码实现,参见:WordPress 代码实现文章标题栏自动添加原创标识

《WordPress 代码实现文章自动添加原创或转载版权声明》《WordPress 代码实现文章自动添加原创或转载版权声明》《WordPress 代码实现文章自动添加原创或转载版权声明》

相关文章

点赞
  1. Thank you a lot for providing individuals with an exceptionally breathtaking chance to read from here. It is usually so excellent and packed with a good time for me and my office mates to search the blog at minimum three times weekly to study the latest stuff you have. Not to mention, I am also usually motivated for the great guidelines you give. Certain 1 tips on this page are in reality the most impressive we've had.

  2. I want to get across my admiration for your kind-heartedness for those people who need guidance on this one niche. Your special dedication to passing the message throughout appears to be astonishingly useful and has frequently permitted many people just like me to realize their endeavors. The interesting guide implies a lot to me and additionally to my colleagues. With thanks; from all of us.

  3. red bottom shoes说道:

    I am only writing to make you understand of the really good experience my cousin's princess developed using your web page. She came to find a lot of things, not to mention what it is like to have an incredible coaching spirit to get a number of people without problems completely grasp chosen tortuous topics. You undoubtedly surpassed her expected results. Thanks for rendering such interesting, dependable, informative and also easy thoughts on your topic to Gloria.

  4. crazy explosive说道:

    I am also commenting to make you know what a perfect experience my wife's princess experienced browsing your site. She noticed such a lot of things, most notably what it is like to possess an ideal helping spirit to get others completely fully understand several hard to do matters. You truly did more than readers' expected results. Many thanks for presenting such great, trusted, explanatory and even easy guidance on that topic to Evelyn.

  5. golden goose说道:

    I and my buddies have been reviewing the good points located on your site and so unexpectedly came up with a horrible suspicion I never expressed respect to the website owner for those techniques. The young men had been very interested to learn them and have sincerely been having fun with these things. I appreciate you for indeed being considerably kind as well as for getting these kinds of great useful guides millions of individuals are really eager to discover. My very own sincere apologies for not saying thanks to you earlier.

  6. retro jordans说道:

    I intended to put you this very small note just to give thanks as before with your remarkable pointers you've contributed on this site. It was quite seriously generous with you to allow extensively all many individuals would've sold for an e-book to help with making some money for themselves, most notably since you could have done it if you ever desired. The suggestions as well worked to become a fantastic way to be sure that many people have similar fervor the same as my personal own to know the truth many more when considering this condition. I'm certain there are lots of more fun sessions in the future for many who looked at your site.

  7. nike roshe说道:

    Needed to draft you the very little observation to finally say thanks a lot again considering the extraordinary principles you've provided above. This is simply extremely open-handed with people like you to grant easily what many people could have marketed for an ebook in making some dough for their own end, particularly seeing that you could have done it in the event you desired. Those concepts in addition acted to become a fantastic way to recognize that someone else have similar keenness just like my personal own to learn way more related to this matter. I think there are lots of more pleasant moments ahead for individuals who find out your blog post.

  8. air max说道:

    I simply wished to say thanks once again. I do not know the things I would've created in the absence of those smart ideas shared by you relating to my question. It was a very challenging scenario in my circumstances, but finding out a professional style you managed the issue made me to cry for contentment. I am just happy for this advice and even trust you are aware of a powerful job you were doing instructing other individuals all through your site. I am certain you've never come across all of us.

  9. My wife and i have been absolutely comfortable when Jordan could round up his web research out of the precious recommendations he had when using the web page. It's not at all simplistic to simply choose to be offering helpful tips which the rest could have been making money from. We figure out we now have the website owner to be grateful to because of that. Those illustrations you made, the easy blog navigation, the relationships you will make it possible to foster - it is many fantastic, and it's making our son and our family consider that that topic is enjoyable, which is certainly wonderfully vital. Thanks for everything!

  10. retro jordans说道:

    I have to convey my love for your generosity for those people who need help on the question. Your very own dedication to passing the solution across turned out to be extraordinarily valuable and have truly helped individuals just like me to attain their targets. This helpful publication entails a lot to me and especially to my peers. Best wishes; from everyone of us.

  11. jordan 4说道:

    I precisely wished to thank you very much all over again. I do not know what I could possibly have taken care of without those secrets provided by you concerning such field. It was actually the hard problem for me, nevertheless encountering the very specialised fashion you solved that made me to leap with joy. I'm just happy for this support and then trust you realize what a great job you were providing teaching the rest through your blog post. I'm certain you haven't encountered any of us.

  12. nfl store说道:

    I am only writing to let you know of the outstanding experience our child developed checking the blog. She even learned too many pieces, most notably what it is like to have a very effective helping nature to make other folks without hassle thoroughly grasp several grueling matters. You truly did more than my expectations. Many thanks for supplying the insightful, trustworthy, revealing not to mention cool tips about the topic to Kate.

  13. I have to show my affection for your kind-heartedness in support of those people who absolutely need assistance with this particular area of interest. Your real dedication to passing the message all through ended up being extremely interesting and have usually encouraged professionals like me to realize their pursuits. Your entire warm and friendly help and advice denotes a great deal to me and even more to my office workers. Thanks a lot; from all of us.

  14. pure boost说道:

    I as well as my friends appeared to be checking out the nice tricks from your web site then suddenly I got a horrible feeling I had not expressed respect to the blog owner for those secrets. These guys were for that reason very interested to learn all of them and now have surely been enjoying them. Thanks for being so accommodating and for utilizing this form of fine useful guides millions of individuals are really desperate to know about. Our own honest apologies for not expressing appreciation to you earlier.

  15. I and also my guys have already been analyzing the best points on your web site and then all of the sudden came up with a horrible feeling I never thanked you for those strategies. Most of the guys became so excited to read through them and already have very much been using those things. Many thanks for simply being indeed helpful and then for having this form of brilliant guides millions of individuals are really desirous to know about. My sincere regret for not saying thanks to sooner.

  16. air jordan 11说道:

    I am glad for writing to make you be aware of of the superb experience my friend's child developed viewing your blog. She realized too many things, which include what it's like to possess a great helping spirit to let most people easily completely grasp a variety of very confusing subject areas. You truly did more than our own expected results. I appreciate you for showing these helpful, safe, informative as well as easy tips about that topic to Sandra.

  17. My husband and i were really lucky when Peter managed to conclude his investigations through the ideas he gained from your web site. It's not at all simplistic to just possibly be giving for free solutions that many people could have been trying to sell. Therefore we take into account we've got you to give thanks to because of that. The illustrations you've made, the straightforward web site menu, the friendships your site make it easier to instill - it's everything fabulous, and it's letting our son in addition to our family recognize that that idea is cool, and that is particularly essential. Many thanks for everything!

  18. nike max说道:

    I am writing to make you understand what a helpful experience my cousin's child encountered checking your blog. She noticed too many details, most notably what it is like to possess a great coaching mindset to make the others without hassle comprehend certain specialized subject matter. You actually exceeded her expected results. I appreciate you for offering those essential, dependable, edifying and in addition cool thoughts on this topic to Julie.

  19. balenciaga shoes说道:

    I simply wanted to write down a simple comment in order to say thanks to you for some of the stunning strategies you are showing on this website. My particularly long internet lookup has finally been compensated with reasonable facts and strategies to exchange with my family. I 'd express that most of us site visitors are definitely fortunate to dwell in a very good community with so many wonderful individuals with interesting strategies. I feel truly grateful to have discovered the site and look forward to some more excellent moments reading here. Thanks once more for everything.

  20. moncler sale说道:

    I really wanted to construct a comment so as to thank you for those nice guides you are placing at this site. My extended internet research has at the end of the day been compensated with professional strategies to share with my relatives. I 'd admit that many of us visitors actually are quite lucky to live in a superb site with very many brilliant professionals with useful tactics. I feel really lucky to have encountered the web site and look forward to tons of more entertaining minutes reading here. Thanks a lot once more for a lot of things.

  21. webhosting说道:

    I was excited to uncover this site. I wanted to thank you
    for your time due to this fantastic read!!
    I definitely enjoyed every part of it and I have you book-marked to check out new things in your website.

  22. sex cam2cam说道:

    Hi there! This is kind of off topic but I need some advice from an established blog.
    Is it very hard to set up your own blog? I'm not very techincal but I can figure things out pretty fast.
    I'm thinking about setting up my own but I'm not sure where to start.
    Do you have any ideas or suggestions? With thanks

  23. Thanks a lot for providing individuals with remarkably nice chance to read from this web site. It really is very good plus full of a good time for me personally and my office peers to visit your site more than thrice per week to find out the newest issues you have. Not to mention, I'm also certainly satisfied considering the tremendous advice you serve. Certain 2 tips in this posting are truly the most beneficial I have ever had.

  24. nike air force 1说道:

    I definitely wanted to jot down a brief remark so as to appreciate you for these fabulous items you are sharing on this site. My time-consuming internet lookup has at the end of the day been honored with good quality suggestions to talk about with my good friends. I 'd state that that many of us site visitors actually are definitely blessed to live in a really good website with so many wonderful individuals with helpful tips and hints. I feel quite happy to have come across your entire webpages and look forward to many more fabulous moments reading here. Thank you again for everything.

  25. air max 270说道:

    I precisely had to thank you very much all over again. I am not sure what I could possibly have made to happen without these advice documented by you on such a theme. It absolutely was the hard setting for me personally, nevertheless looking at this specialized technique you handled it forced me to jump for joy. I will be happy for this service and as well , wish you really know what a great job you're putting in teaching other individuals by way of your website. Most likely you've never encountered all of us.

  26. yeezy shoes说道:

    I really wanted to type a simple message in order to appreciate you for the marvelous tips and tricks you are giving out at this site. My considerable internet research has at the end of the day been rewarded with reliable insight to exchange with my partners. I would repeat that we visitors actually are very much endowed to exist in a wonderful community with many marvellous individuals with helpful secrets. I feel quite grateful to have encountered your entire web pages and look forward to plenty of more awesome minutes reading here. Thanks a lot again for all the details.

  27. nike flyknit说道:

    Thank you a lot for providing individuals with an exceptionally remarkable chance to read in detail from this site. It is often very excellent and as well , jam-packed with a lot of fun for me and my office colleagues to visit your site at the very least 3 times weekly to see the latest stuff you will have. And lastly, we're usually impressed with all the outstanding concepts you serve. Some 1 ideas in this article are absolutely the very best I have had.

  28. Sannywrart说道:

    Make a more new posts please :)
    ___
    Sanny

  29. wjtree说道:

    you can try Wordfence Security, the website is https://wordpress.org/plugins/wordfence/

  30. plastik cor说道:

    Hello! Do you know if they make any plugins to protect against hackers?
    I'm kinda paranoid about losing everything I've worked hard on. Any suggestions?

  31. Ιf you want to get much frokm this parаgraph then you haᴠe to аpply such techniques to yoіг won weƅ
    site.

发表评论

电子邮件地址不会被公开。