<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>睡到25点 &#187; API</title>
	<atom:link href="http://www.voidman.com/tag/api/feed" rel="self" type="application/rss+xml" />
	<link>http://www.voidman.com</link>
	<description>个人博客，记录与分享。</description>
	<lastBuildDate>Thu, 12 Jan 2012 14:00:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>让 WordPress 自动删除 Post Revisions</title>
		<link>http://www.voidman.com/2008/11/automatically-delete-post-revisions.html</link>
		<comments>http://www.voidman.com/2008/11/automatically-delete-post-revisions.html#comments</comments>
		<pubDate>Tue, 18 Nov 2008 01:00:52 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Blog Related]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[revisions]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WP_Cron]]></category>
		<category><![CDATA[计划任务]]></category>

		<guid isPermaLink="false">http://www.voidman.com/?p=188</guid>
		<description><![CDATA[貌似在 wp-config.php 中加入 define(’WP_POST_REVISIONS’, false); 来禁用 WordPress 的日志修订功能，post revision 还是会产生。gohsy 同学写了个插件 Revision Manager 来清理 post revision，不过个人觉得手动清理还是不够方便，决定利用 WordPress 的计划任务功能(WP_Cron)偷偷懒。 不想为了这小小的功能而多添加一个插件，所以在主题目录下的 functions.php 文件添加了以下代码： function delete_post_revisions&#40;&#41; &#123; global $wpdb; &#160; // Also need to delete the post meta and term relationships $wpdb-&#62;query&#40;&#34;DELETE FROM &#8230; <a href="http://www.voidman.com/2008/11/automatically-delete-post-revisions.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>貌似在 <code>wp-config.php</code> 中加入 <code>define(’WP_POST_REVISIONS’, false);</code> 来禁用 WordPress 的日志修订功能，post revision 还是会产生。<a href="http://blog.gohsy.com/" rel="external nofollow">gohsy</a> 同学写了个插件 <a href="http://blog.gohsy.com/topics/delete-revision-plugin.html" rel="external nofollow">Revision Manager</a> 来清理 post revision，不过个人觉得手动清理还是不够方便，决定利用 WordPress 的计划任务功能(WP_Cron)偷偷懒。</p>
<p>不想为了这小小的功能而多添加一个插件，所以在主题目录下的 <code>functions.php</code> 文件添加了以下代码：</p>

<div class="wp_syntax"><pre class="php"><span style="color: blue;">function</span> delete_post_revisions<span style="color: #800000;">&#40;</span><span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#123;</span>
  <span style="color: blue;">global</span> <span style="color: #008080;">$wpdb</span>;
&nbsp;
  <span style="color: #008000; font-style: italic;">// Also need to delete the post meta and term relationships</span>
  <span style="color: #008080;">$wpdb</span>-&gt;<span style="color: #006600;">query</span><span style="color: #800000;">&#40;</span><span style="color: #ff00ff;">&quot;DELETE FROM {$wpdb-&gt;postmeta} WHERE post_id IN (SELECT ID FROM {$wpdb-&gt;posts} WHERE post_type = 'revision')&quot;</span><span style="color: #800000;">&#41;</span>;
  <span style="color: #008080;">$wpdb</span>-&gt;<span style="color: #006600;">query</span><span style="color: #800000;">&#40;</span><span style="color: #ff00ff;">&quot;DELETE FROM {$wpdb-&gt;term_relationships} WHERE object_id IN (SELECT ID FROM {$wpdb-&gt;posts} WHERE post_type = 'revision')&quot;</span><span style="color: #800000;">&#41;</span>;
&nbsp;
  <span style="color: #008000; font-style: italic;">// Delete the post revisions</span>
  <span style="color: #008080;">$wpdb</span>-&gt;<span style="color: #006600;">query</span><span style="color: #800000;">&#40;</span><span style="color: #ff00ff;">&quot;DELETE FROM {$wpdb-&gt;posts} WHERE post_type = 'revision'&quot;</span><span style="color: #800000;">&#41;</span>;
<span style="color: #800000;">&#125;</span>
&nbsp;
<span style="color: #008000; font-style: italic;">// Register the event</span>
add_action<span style="color: #800000;">&#40;</span><span style="color: #ff00ff;">'delete_post_revisions_event'</span>, <span style="color: #ff00ff;">'delete_post_revisions'</span><span style="color: #800000;">&#41;</span>;
<span style="color: blue;">if</span> <span style="color: #800000;">&#40;</span>!wp_next_scheduled<span style="color: #800000;">&#40;</span><span style="color: #ff00ff;">'delete_post_revisions_event'</span><span style="color: #800000;">&#41;</span><span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#123;</span>
  wp_schedule_event<span style="color: #800000;">&#40;</span><span style="color: #ff0000;">time</span><span style="color: #800000;">&#40;</span><span style="color: #800000;">&#41;</span>, <span style="color: #ff00ff;">'daily'</span>, <span style="color: #ff00ff;">'delete_post_revisions_event'</span><span style="color: #800000;">&#41;</span>;
<span style="color: #800000;">&#125;</span></pre></div>

<p><span id="more-188"></span><br />
这样 WordPress 每天会自动删除 post revision (当然得在有人访问的前提下)。其实每天运行一次也有些过于频繁了，可以将时间间隔设置得更长一些，譬如一周或者二周甚至一个月。不过 WordPress 的 Cron API 本身只提供了 hourly, daily 两个选项，所以需要自己手工添加一些运行时间间隔选项。在刚才添加的代码<strong>之前</strong>添加以下代码，</p>

<div class="wp_syntax"><pre class="php"><span style="color: blue;">function</span> add_schedule_options<span style="color: #800000;">&#40;</span><span style="color: #008080;">$schedules</span><span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#123;</span>
  <span style="color: #008080;">$custom_schedules</span> = <span style="color: blue;">array</span><span style="color: #800000;">&#40;</span>
    <span style="color: #ff00ff;">'weekly'</span> =&gt; <span style="color: blue;">array</span><span style="color: #800000;">&#40;</span> <span style="color: #ff00ff;">'interval'</span> =&gt; <span style="color: #800080;">604800</span>, <span style="color: #ff00ff;">'display'</span> =&gt; __<span style="color: #800000;">&#40;</span><span style="color: #ff00ff;">'Once Weekly'</span><span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#41;</span>, 
    <span style="color: #ff00ff;">'fortnightly'</span> =&gt; <span style="color: blue;">array</span><span style="color: #800000;">&#40;</span> <span style="color: #ff00ff;">'interval'</span> =&gt; <span style="color: #800080;">1209600</span>, <span style="color: #ff00ff;">'display'</span> =&gt; __<span style="color: #800000;">&#40;</span><span style="color: #ff00ff;">'Once Fortnightly'</span><span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#41;</span>, 
    <span style="color: #ff00ff;">'monthly'</span> =&gt; <span style="color: blue;">array</span><span style="color: #800000;">&#40;</span> <span style="color: #ff00ff;">'interval'</span> =&gt; <span style="color: #800080;">2592000</span>, <span style="color: #ff00ff;">'display'</span> =&gt; __<span style="color: #800000;">&#40;</span><span style="color: #ff00ff;">'Once Monthly'</span><span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#41;</span>, 
  <span style="color: #800000;">&#41;</span>;
&nbsp;
  <span style="color: blue;">return</span> <span style="color: #ff0000;">array_merge</span><span style="color: #800000;">&#40;</span><span style="color: #008080;">$custom_schedules</span>, <span style="color: #008080;">$schedules</span><span style="color: #800000;">&#41;</span>;
<span style="color: #800000;">&#125;</span>
add_filter<span style="color: #800000;">&#40;</span><span style="color: #ff00ff;">'cron_schedules'</span>, <span style="color: #ff00ff;">'add_schedule_options'</span><span style="color: #800000;">&#41;</span>;</pre></div>

<p>然后把 <code>wp_schedule_event</code> 函数的第二个参数值 daily 改成 weekly 或  fortnightly 或 monthly 即可。</p>
<h4>Related Posts</h4><ul class="related_post"><li><a href="http://www.voidman.com/2009/05/using-wordpress-plugin-api-hack-discuz.html" title="WordPress 的 Hook 机制在 Discuz 二次开发中的应用">WordPress 的 Hook 机制在 Discuz 二次开发中的应用</a></li><li><a href="http://www.voidman.com/2009/04/at-reply-mail-notification-1-release.html" title="At Reply Mail Notification 1.0 Release">At Reply Mail Notification 1.0 Release</a></li><li><a href="http://www.voidman.com/2009/03/lbs-to-wordpress.html" title="LBS 转 WordPress 不完全记录">LBS 转 WordPress 不完全记录</a></li><li><a href="http://www.voidman.com/2009/01/wordpress-is-using-a-bad-way-to-detect-duplicate-comment.html" title="Wordpress 糟糕的重复评论检测方式">Wordpress 糟糕的重复评论检测方式</a></li><li><a href="http://www.voidman.com/2009/01/at-reply-mail-notification-test.html" title="At Reply Mail Notification 测试贴">At Reply Mail Notification 测试贴</a></li><li><a href="http://www.voidman.com/2008/11/google-xml-sitemaps-ignore-the-specified-categories.html" title="让 Google Sitemaps Generator 可以忽略指定的日志分类">让 Google Sitemaps Generator 可以忽略指定的日志分类</a></li><li><a href="http://www.voidman.com/2008/11/the-better-solution-for-static-paged-post.html" title="Wordpress 分页文章静态化的更优解决方案">Wordpress 分页文章静态化的更优解决方案</a></li><li><a href="http://www.voidman.com/2008/07/upgraded-to-wordpress-v26.html" title="升级到 WordPress 2.6">升级到 WordPress 2.6</a></li><li><a href="http://www.voidman.com/2008/03/upgraded-to-wordpress-25-rc1.html" title="升级到 WordPress 2.5 RC1">升级到 WordPress 2.5 RC1</a></li><li><a href="http://www.voidman.com/2008/03/wordpress-theme-wpcandy-remix.html" title="WordPress Theme WPCandy Remix">WordPress Theme WPCandy Remix</a></li></ul><hr /><a href="http://www.voidman.com">Voidman.com</a> | Link: <a href="http://www.voidman.com/2008/11/automatically-delete-post-revisions.html">http://www.voidman.com/2008/11/automatically-delete-post-revisions.html</a><br /><img alt="linezing" width="1" height="1" src="http://img.tongji.linezing.com/288120/tongji.gif" />]]></content:encoded>
			<wfw:commentRss>http://www.voidman.com/2008/11/automatically-delete-post-revisions.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

