程序员的世界

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. nike react说道:

    The subsequent time I learn a blog, I hope that it doesnt disappoint me as a lot as this one. I imply, I do know it was my option to learn, however I really thought youd have one thing fascinating to say. All I hear is a bunch of whining about one thing that you could possibly fix if you happen to werent too busy on the lookout for attention.

  2. yeezy boost说道:

    I just wanted to compose a brief comment to appreciate you for those amazing steps you are showing on this website. My extended internet investigation has at the end been paid with wonderful tips to go over with my best friends. I 'd mention that we site visitors are undoubtedly endowed to exist in a great place with many lovely people with useful hints. I feel pretty privileged to have encountered your website and look forward to some more entertaining times reading here. Thanks again for all the details.

  3. cheap jordans说道:

    There's noticeably a bundle to learn about this. I assume you made certain nice points in options also.

  4. ferragamo belt说道:

    Thanks for all of the work on this site. My mother loves doing investigations and it is simple to grasp why. We know all about the powerful tactic you provide advantageous tips via the website and therefore inspire contribution from other ones on this article plus our favorite child is actually learning a lot of things. Have fun with the rest of the new year. You're performing a stunning job.

  5. iniki说道:

    I have to express my gratitude for your kindness for persons that actually need help with this important niche. Your very own commitment to passing the solution around appears to be remarkably beneficial and has usually empowered individuals just like me to get to their pursuits. Your entire helpful useful information denotes a lot to me and further more to my peers. Thank you; from all of us.

  6. Anja说道:

    Highly energetic post, I liked that a lot. Will
    there be a part 2?

  7. I have to point out my respect for your generosity in support of folks that absolutely need help on this one subject. Your very own dedication to getting the solution across was amazingly functional and have all the time encouraged somebody just like me to realize their dreams. Your entire informative instruction implies so much to me and even further to my office colleagues. Thanks a ton; from all of us.

  8. I just wanted to make a brief word in order to say thanks to you for these great pointers you are writing on this site. My time consuming internet look up has at the end of the day been compensated with high-quality facts and techniques to write about with my classmates and friends. I would express that we visitors actually are truly lucky to exist in a useful place with very many lovely individuals with helpful strategies. I feel truly blessed to have seen the web pages and look forward to many more excellent minutes reading here. Thanks again for everything.

  9. jordan 13说道:

    I must express some appreciation to you just for bailing me out of this scenario. After surfing around throughout the internet and obtaining strategies which are not beneficial, I thought my entire life was done. Being alive devoid of the solutions to the problems you've sorted out all through your good article content is a critical case, as well as those which might have badly affected my career if I hadn't encountered the blog. The expertise and kindness in taking care of all the stuff was very useful. I don't know what I would have done if I had not encountered such a point like this. I can also at this time look ahead to my future. Thank you very much for the expert and sensible help. I won't think twice to endorse your blog post to any individual who should receive tips about this subject.

  10. off white说道:

    I wanted to post you that very little observation to thank you so much over again for those awesome ideas you've provided here. This is really shockingly generous of you giving without restraint what exactly many people could have advertised for an e-book to earn some money for their own end, particularly now that you might well have tried it if you wanted. Those things likewise worked to become a great way to be sure that someone else have the same dream really like my own to grasp more in terms of this condition. I believe there are some more pleasant opportunities ahead for folks who read your blog post.

  11. adidas nmd说道:

    I needed to post you that little bit of word to finally thank you so much the moment again just for the incredible tips you have documented here. It's simply extremely open-handed of people like you to supply extensively what exactly many of us would've offered for an electronic book to get some money on their own, primarily considering the fact that you could possibly have tried it in the event you decided. These secrets also acted to become great way to fully grasp that most people have the identical interest just as my personal own to know great deal more on the topic of this problem. I know there are a lot more pleasant opportunities up front for individuals that browse through your blog.

  12. pandora bracelet说道:

    I simply wanted to construct a small note so as to express gratitude to you for all of the splendid guides you are showing on this site. My particularly long internet lookup has now been recognized with extremely good strategies to exchange with my family members. I would assume that we site visitors actually are undoubtedly blessed to dwell in a notable website with so many lovely individuals with great plans. I feel truly privileged to have seen the web site and look forward to plenty of more fabulous moments reading here. Thank you again for a lot of things.

  13. air force 1说道:

    I have to express my thanks to the writer just for bailing me out of such a predicament. As a result of looking out through the world-wide-web and finding ideas that were not pleasant, I believed my life was well over. Living minus the answers to the issues you have fixed through your good write-up is a crucial case, as well as those which may have badly damaged my entire career if I had not discovered your web page. Your primary knowledge and kindness in taking care of every item was very useful. I don't know what I would have done if I had not come upon such a step like this. I can also at this moment look forward to my future. Thank you very much for your skilled and results-oriented help. I will not hesitate to recommend your web page to anybody who wants and needs guide on this subject matter.

  14. jordan shoes说道:

    My wife and i felt now lucky that Albert managed to round up his analysis via the ideas he came across from your web pages. It is now and again perplexing just to always be releasing facts which usually many others could have been trying to sell. And we do know we have the blog owner to appreciate for that. The explanations you have made, the straightforward website navigation, the relationships you will help to create - it's got most unbelievable, and it is making our son in addition to the family imagine that this idea is cool, which is certainly highly fundamental. Thank you for everything!

  15. hermes belt说道:

    I wanted to compose you that tiny note to help thank you over again on the precious methods you've shown in this article. This is so remarkably open-handed with people like you to offer extensively all that most of us might have sold as an electronic book to end up making some cash on their own, principally considering the fact that you might well have done it if you ever desired. The secrets also worked to be a great way to understand that most people have the identical dream like mine to see a good deal more pertaining to this problem. I'm sure there are some more fun times ahead for individuals that look into your blog.

  16. I enjoy you because of all your efforts on this website. Ellie really loves making time for internet research and it's really easy to see why. A number of us notice all regarding the lively form you produce important tips and hints on this web blog and even increase response from website visitors on this matter and our daughter is without a doubt learning a whole lot. Take pleasure in the remaining portion of the new year. Your conducting a really good job.

  17. My spouse and i ended up being thankful that Emmanuel managed to conclude his researching out of the precious recommendations he received out of your weblog. It's not at all simplistic just to continually be offering instructions which men and women may have been trying to sell. So we acknowledge we have got the website owner to give thanks to for this. The specific illustrations you made, the straightforward site menu, the friendships you can help create - it's got most fantastic, and it is aiding our son and our family consider that that subject is fun, which is wonderfully indispensable. Thanks for everything!

  18. Thank you a lot for giving everyone an extraordinarily brilliant opportunity to read in detail from this web site. It really is very terrific and as well , full of amusement for me and my office friends to search your website particularly 3 times every week to see the latest secrets you will have. And definitely, I am usually motivated considering the magnificent opinions you give. Some 3 points on this page are without a doubt the simplest I've ever had.

  19. ultra boost 3.0说道:

    I wish to express my appreciation for your kindness in support of individuals that absolutely need help on this one theme. Your personal commitment to passing the message along turned out to be certainly useful and have surely enabled ladies like me to reach their pursuits. The interesting publication means a whole lot a person like me and further more to my colleagues. Many thanks; from all of us.

  20. ysl handbags说道:

    A lot of thanks for your own efforts on this blog. Betty takes pleasure in doing internet research and it's easy to understand why. A lot of people learn all regarding the powerful manner you offer functional tips and tricks via your website and therefore boost participation from some other people about this idea then our daughter is without a doubt starting to learn so much. Take pleasure in the remaining portion of the new year. You are doing a great job.

  21. cheap jordans说道:

    My husband and i ended up being quite ecstatic Michael could complete his research via the precious recommendations he grabbed out of your site. It's not at all simplistic just to continually be giving away information that other folks have been trying to sell. Therefore we keep in mind we have got the website owner to be grateful to for that. Those explanations you've made, the easy site navigation, the friendships you can give support to instill - it's got many remarkable, and it is leading our son in addition to our family understand that content is satisfying, and that's wonderfully serious. Thanks for everything!

  22. goyard handbags说道:

    I simply wanted to thank you so much once more. I do not know the things I would have worked on in the absence of the actual opinions shown by you concerning this question. It absolutely was the depressing setting in my opinion, however , considering a specialised form you processed the issue forced me to cry for fulfillment. I am just happier for your guidance and in addition expect you realize what a great job that you are doing educating men and women via a blog. Most likely you have never encountered all of us.

  23. Thank you for your whole hard work on this blog. My niece takes pleasure in engaging in research and it's obvious why. A number of us know all regarding the dynamic mode you convey helpful steps via your web blog and in addition encourage contribution from other people on the idea and our favorite daughter is undoubtedly being taught a whole lot. Take advantage of the remaining portion of the year. You have been doing a tremendous job.

  24. nmd说道:

    Thanks a lot for providing individuals with an extraordinarily superb chance to read critical reviews from this website. It is always so cool and full of a great time for me and my office mates to visit your site at a minimum three times in 7 days to learn the fresh things you will have. Of course, I'm so certainly contented concerning the striking information you give. Selected 4 tips in this post are absolutely the best I have ever had.

  25. nike hyperdunk说道:

    I would like to express appreciation to this writer for rescuing me from such a difficulty. Right after researching through the internet and coming across basics that were not pleasant, I thought my entire life was well over. Living devoid of the strategies to the issues you've sorted out through your entire write-up is a crucial case, as well as those that might have negatively affected my career if I had not come across your web page. Your personal knowledge and kindness in playing with every item was precious. I'm not sure what I would've done if I had not come across such a point like this. I can now look ahead to my future. Thank you very much for this specialized and effective help. I will not think twice to endorse the website to anyone who wants and needs care about this topic.

  26. I precisely had to thank you so much all over again. I am not sure the things I could possibly have handled without the entire ideas revealed by you directly on this area of interest. Entirely was a real distressing situation for me personally, however , spending time with the very specialized form you managed that forced me to jump over contentment. I'm happy for your information and in addition hope that you comprehend what an amazing job you were carrying out instructing some other people through the use of a site. Most likely you have never encountered all of us.

  27. 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.

  28. 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.

  29. 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.

  30. 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.

  31. 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.

  32. 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.

  33. 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.

  34. 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.

  35. 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!

  36. 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.

  37. 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.

  38. 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.

  39. 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.

  40. 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.

  41. 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.

  42. 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.

  43. 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!

  44. 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.

  45. 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.

  46. 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.

  47. 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.

  48. 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

  49. 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.

  50. 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.

  51. 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.

  52. 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.

  53. 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.

  54. Sannywrart说道:

    Make a more new posts please :)
    ___
    Sanny

  55. wjtree说道:

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

  56. 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?

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