无为而我 發表於 2023-7-2 00:00:00

WordPress中用于更新伪静态规则的PHP代码实例讲解

<p>
flush_rewrite_rules() 函数用来删除然后根据现有的条件重写伪静态规则,也就是刷新一次伪静态规则了。</p>
<p>
先来说一下,通常在主题或者插件添加新的自定义文章类型的时候调用,防止新的自定义文章类型的文章出现 404 的情况,或者很多时候我们都需要在主题启用的时候执行一些代码,比如布置一些数据库表单、跳转到设置页面等等,WordPress 本身并没有提供相关的钩子,网上也有很多五花八门的实现方法,经过我的研究,发现了可能是最优的方法,下边分享给大家:</p>
<div>
<div>
<div id="highlighter_754302">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
<div>
4</div>
<div>
5</div>
<div>
6</div>
<div>
7</div>
<div>
8</div>
<div>
9</div>
<div>
10</div>
<div>
11</div>
<div>
12</div>
<div>
13</div>
</td>
<td>
<div>
<div>
<code>/**</code>
</div>
<div>
<code>  </code><code>*WordPress 在主题启用的时候执行一些代码</code>
</div>
<div>
<code>  </code><code>*http://www.endskin.com/theme-activation-action/</code>
</div>
<div>
<code>*/</code>
</div>
<div>
<code>function</code> <code>Bing_theme_activation(){</code>
</div>
<div>
<code>  </code><code>if</code><code>( </code><code>$GLOBALS</code><code>[</code><code>'pagenow'</code><code>] != </code><code>'themes.php'</code> <code>|| !isset( </code><code>$_GET</code><code>[</code><code>'activated'</code><code>] ) ) </code><code>return</code><code>;</code>
</div>
<div>
<code>  </code><code>/*</code>
</div>
<div>
<code>  </code><code>这里就可以放置在主题启用的时候要执行的代码了,比如跳转到设置界面:</code>
</div>
<div>
<code>  </code><code>wp_redirect( admin_url( 'options-general.php' ) );//注意修改页面地址</code>
</div>
<div>
<code>  </code><code>die;</code>
</div>
<div>
<code>  </code><code>*/</code>
</div>
<div>
<code>}</code>
</div>
<div>
<code>add_action( </code><code>'load-themes.php'</code><code>, </code><code>'Bing_theme_activation'</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
此代码放在主题和插件里都是有效的。</p>
<p>
另外要注意,更新伪静态规则是非常消耗时间和效率的,所以不要每次执行代码都调用,只在必要的情况调用(比如启用主题和启用插件),把 flush_rewrite_rules() 挂到 init 钩子上是极其不正确的。</p>
<p>
用法</p>
<div>
<div>
<div id="highlighter_957093">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>flush_rewrite_rules( </code><code>$hard</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
参数</p>
<p>
$hard</p>
<p>
(布尔)(可选)如果为 True 则一起刷新 .htaccess 文件(hard flush);为 False 则只更新数据库里的伪静态规则(soft flush)。</p>
<p>
默认值:True(hard flush)。</p>
<p>
例子</p>
<p>
在主题启用的时候更新伪静态规则:</p>
<div>
<div>
<div id="highlighter_24700">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
<div>
4</div>
<div>
5</div>
</td>
<td>
<div>
<div>
<code>function</code> <code>Bing_theme_activation(){</code>
</div>
<div>
<code>  </code><code>if</code><code>( </code><code>$GLOBALS</code><code>[</code><code>'pagenow'</code><code>] != </code><code>'themes.php'</code> <code>|| !isset( </code><code>$_GET</code><code>[</code><code>'activated'</code><code>] ) ) </code><code>return</code><code>;</code>
</div>
<div>
<code>  </code><code>flush_rewrite_rules();</code>
</div>
<div>
<code>}</code>
</div>
<div>
<code>add_action( </code><code>'load-themes.php'</code><code>, </code><code>'Bing_theme_activation'</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
在插件启用的时候更新伪静态规则:</p>
<div>
<div>
<div id="highlighter_393335">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
<div>
4</div>
</td>
<td>
<div>
<div>
<code>function</code> <code>Bing_myplugin_activate(){</code>
</div>
<div>
<code>  </code><code>flush_rewrite_rules();</code>
</div>
<div>
<code>}</code>
</div>
<div>
<code>register_activation_hook( </code><code>__FILE__</code><code>, </code><code>'Bing_myplugin_activate'</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
<br>
其它</p>
<p>
此函数位于:wp-includes/rewrite.php</p>
頁: [1]
查看完整版本: WordPress中用于更新伪静态规则的PHP代码实例讲解