Posts Tagged ‘wordpress’

A easy way to add copyright information automatically into post(English Version英文版)

 

Several weeks ago, I decided to write about two blog posts every day. I used to write copyright information manually in the post. Apparently, it's better to let the machine do this type of trivial work automatically. So, I searched the plugins to try to find a convenient one. Indeed, I found the plugin named "add post URL" which is able to help the author to insert copyright information into the post. When I clicked the 'DETAIL' button to see more. A warning message popped up: "This plugin has not been tested with your current version of WordPress". I was scared. Because I once had a bad experience about how untested plugin ruined my wordpress as a whole. So, the option of using plugin to insert copyright information was ruled out.

 

Then, I tried to modify wordpress's sourcecode by myself. I googled some related post taking about how to do this kind of work. One article suggested to modify the single.php file, which I am not very sure if the file name will change depends on different themes you use. I reckon the file name will be like the style of "single.php", more or less. Now, you locate the following code in single.php.

 

<?php the_content(); ?>

<!--
Pagination for Multi-page posts
~~~ -->
<?php wp_link_pages('before=<p class="multi-page">Pages:&after=</p>'); // if this is a multipage post then show the navigation ?>
 
As long as you've found it, insert code that generate copyright information below the line <?php the_content(); ?>. Unfortunately, many people will soon find this is not going to work. Since many other plugins has already add texts into the post content. Take my blog for example, I use the plugin to automatically generate related posts list at the bottom of every post. So, in this way, copyright information will be put right below the related posts list, rather than expected right below the real post content. It is definitely undesirable. 
 
I did google more and found a solution. One article suggest to modify function.php and add a customer defined function to replace the original the_content() function in the single.php file. I read the code and found it's not easy to comprehend for a normal person who are not familiar with wordpress's code. I didn't understand the parameters transferred to the function at least. What is more, I greatly doubted this kind of solution will collide with my plugin generating related posts list.
 
Everything seemed to get stuck. I went out to have my supper and have some kind of fresh air. When I returned to the front of the computer. I suddenly figured out a perfect solution of add copyright info into my post without using plugin and heavy code. The idea is described as the following steps:
step 1: when you are composing your article, type some kind of special and simple string in the place where you expect to show up the copyright information. Take my blog for exambple, I type "COPYRIGHT   WWW.DOGEYE.NET" at the tail of every post.
step 2: Using php code to replace the special string to anything relatively verbose in the time when the post will be showing.
 
Isn't it a brilliant, simple and flexible idea.
To put this idea into real. You must make it clear that the two function the_content() and get_the_content() behaves differently. When the function the_content() is invoked, the content of the post is being printed out. get_the_content() is different, this function return the article text as a string return value. As you have got this point clear, you should comprehend the following code, which replace the the_content() function with get_the_content() function.
 
<?php
$thread_content = get_the_content();             // variable $thread_content holds the post content.
$thread_permalink = get_permalink();              // get the current post's permanent link.
$thread_title = get_the_title();                         // get the current post's title.
$dogeye_copyright = "<p style='font-weight:bold';><span>Original Article created by</span> <a href='http://blog.ykyi.net' target='_blank'>DogEye.NET</a><br/><span>Repost is only allowed if original URL is reserved.</span><br/><a href=\"$thread_permalink\" target='_blank'>$thread_title<br/>$thread_permalink</a></p>";       // the HTML code that claiming copyright
 
// The following code change the simple string "COPYRIGHT  WWW.DOGEYE.NET" with more verbose HTML code.
$thread_content = str_replace('COPYRIGHT  WWW.DOGEYE.NET', $dogeye_copyright, $thread_content);
 
echo $thread_content;    // print out the modified content.
?>
 
I suppose this piece of code snippet is easy to comprehend with comments. If you think it is useful, you could replace the site-specified string with your version. Hope this post will be helpful.
 
COPYRIGHT WWW.DOGEYE.NET

 

 

Be the first to comment - What do you think?  Posted by zausiu - June 17, 2012 at 13:26

Categories: Tech Articles   Tags:

一个更方便的在wordpress博客的文章中自动添加版权信息的方法(Chinese Version 中文版)

从半个月前,我决定每天都写两篇左右篇客,或者原创,或者翻译。对于国内的抄袭风气,大家都很清楚,转载别人的文章时把别人的版权信息改成自己的。虽然没有非常好的办法对付抄袭。但至少也需要摆明原创作者的态度还是很必要的。

一开始,我在每篇博文里手动添加版权信息和链接。虽然工作量不算太大,但对于这种高度重复性的工作,能让机器做还是交给机器做吧。

首先当然想到的是找相关自动插入版权信息的wordpress插件,虽然也找到一款。名字叫add post,如果没有记错的话!点击Detail查看详细说明的时候发现已经很久没有更新了,而且有一个警告说对于我当前使用的wordpress版本没有经过测试。没有经过测试这句话吓到我了,我不想因为一个插件给我的博客引用不稳定性。于是放弃了使用插件。

 

不能使用插件那就只能自己改代码。谷歌了一下,自然搜索到前人写的相关贴子。贴子介绍说找到single.php文件。single.php文件是发贴发贴的文件,我不确定是不是文件名和所有的主题相关,会不会因为使用不同的主题,这个文件名会不太一样。然后找到这段代码:

<?php the_content(); ?>

<!--
Pagination for Multi-page posts
~~~ -->
<?php wp_link_pages('before=<p class="multi-page">Pages:&after=</p>'); // if this is a multipage post then show the navigation ?>
 
然后在这段代码的<?php the_content(); ?> 下面插入生成版本信息的代码。但问题是,这种方法并不可靠。非常多的博客使用了其它相关的插件会干扰这种做法。比如,我使用了一个自动生成相关文章列表的插件。于是版本信息被加到了相关文章列表下面,离真正的文章正文有很远的距离。这必不令人满意。另有文章指出可以修改function.php文件,自定义一个和the_content()类似的函数,然后把single.php中的the_content()函数替换掉来完成这个任务。我看了一下,不太满意。修改的地方超过了两个文件,觉得改动有点多。另外写的那个自定义函数对于没有太多wordpress的代码经验的人来讲并不是非常容易读懂。至少我就不明白那个自定义函数的参数的意义,应该和wordpress的标准代码相关。另外,我很怀疑如果用了这种方式,可以会使我的显示相关博文列表的插件失效。一下子没有找到非常好的解决方法。于是去吃晚饭。吃完晚饭后回到电脑前突然来了灵感。可以在用替换字符串的方法增加版权信息呀。想法是这样的。
1. 在编辑文章的时候,在你需要插入版本信息的地方加入特殊的字符串。比如我选择了在每篇博文最后敲入字符串 “copyright  blog.ykyi.net”
2. 在发表文章阶段把这个特殊字符串替换成html代码,显示你的版本信息。
是不是既简单又灵活呢!
注意要实现这个想法,需要明白 the_content() 和 get_the_content() 两个wordpress函数的区别。在调用the_content()函数的时候,文章的内容就已经打印出来了。但get_the_content()是把文章的内容做为函数返回值返回。所以在修改single.php文件时,要把 the_content()用get_the_content()函数代替。
下面贴出我的代码。看官如果需要可以修改相应部分:
<?php
$thread_content = get_the_content(); 
$thread_permalink = get_permalink();
$thread_title = get_the_title($post_id);
$dogeye_copyright = "<pre>除非明确声明,本站所以文章均为<a href='http://blog.ykyi.net' target='_blank'>DogEye博客</a>原创或者自主翻译。<br/>如转载,请注明出处。<a href=\"$thread_permalink\" target='_blank'>$thread_title $thread_permalink</a></pre>";
$thread_content = str_replace('copyright  blog.ykyi.net', $dogeye_copyright, $thread_content);
echo $thread_content;
?>
 
代码应该非常容易读懂了。而且你可以非常灵活地控制要插入的位置,还可以灵活决定插入多处版本声明。
 
copyright blog.ykyi.net

 

Be the first to comment - What do you think?  Posted by zausiu - June 15, 2012 at 21:17

Categories: Life and Others, Tech Articles   Tags: