author_rewrite_rules
云策文档标注
概述
author_rewrite_rules 是一个 WordPress 过滤器,用于修改作者归档页面的重写规则。开发者可以通过此过滤器自定义作者归档的 URL 结构,例如添加、修改或删除特定规则。
关键要点
- author_rewrite_rules 过滤器允许开发者修改作者归档的重写规则数组,键为正则表达式模式。
- 使用 add_filter 钩入过滤器,定义回调函数来操作 $author_rewrite 数组,实现自定义规则。
- 修改重写规则后,需要刷新重写规则以生效,可通过访问固定链接设置页面或编程方式调用 flush_rewrite_rules()。
代码示例
add_filter( 'author_rewrite_rules', 'wpdocs_author_rewrite_rules' );
function wpdocs_author_rewrite_rules( $author_rewrite ) {
// 添加自定义重写规则
$author_rewrite['author/([a-zA-Z0-9-]+)/custom'] = 'index.php?author_name=$matches[1]&custom_param=value';
// 修改现有规则
if ( isset( $author_rewrite['author/([a-zA-Z0-9-]+)/?$'] ) ) {
$author_rewrite['author/([a-zA-Z0-9-]+)/?$'] = 'index.php?author_name=$matches[1]&priority=high';
}
// 删除特定规则
unset( $author_rewrite['author/([0-9]+)/?$'] );
return $author_rewrite;
}注意事项
- 刷新重写规则是必要的步骤,否则修改可能不会立即生效。
- 在主题的 functions.php 文件或自定义插件中使用此过滤器,避免直接修改核心文件。
原文内容
Filters rewrite rules used for author archives.
Description
Likely author archives would include /author/author-name/, as well as pagination and feed paths for author archives.
Parameters
$author_rewritestring[]-
Array of rewrite rules for author archives, keyed by their regex pattern.
Source
$author_rewrite = apply_filters( 'author_rewrite_rules', $author_rewrite );
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |
Skip to note 2 content
Noruzzaman
In this tutorial, you will learn how to use the
apply_filtersfunction with theauthor_rewrite_rulesfilter to modify the author rewrite rules based on multiple conditions in WordPress. This can be useful if you want to customize the URL structure for author archives.Step 1 Hook into the Filter:
add_filter( 'author_rewrite_rules', 'wpdocs_author_rewrite_rules' );Step 2 Define the Callback Function:
function wpdocs_author_rewrite_rules( $author_rewrite ) { // Condition 1: Add a custom rewrite rule for specific author nicenames $author_rewrite['author/([a-zA-Z0-9-]+)/custom'] = 'index.php?author_name=$matches[1]&custom;_param=value'; // Condition 2: Modify the priority of an existing rewrite rule if ( isset( $author_rewrite['author/([a-zA-Z0-9-]+)/?$'] ) ) { $author_rewrite['author/([a-zA-Z0-9-]+)/?$'] = 'index.php?author_name=$matches[1]&priority;=high'; } // Condition 3: Remove a specific rewrite rule unset( $author_rewrite['author/([0-9]+)/?$'] ); // Additional customizations can be added here return $author_rewrite; }Step 3: Flush Rewrite Rules
After modifying the rewrite rules, you need to flush the rewrite rules to ensure the changes take effect. You can do this by visiting the Permalinks settings page in the WordPress admin area (Settings > Permalinks) and simply clicking “Save Changes”.
Alternatively, you can flush the rewrite rules programmatically. Add this code to your theme’s
functions.phpfile or your custom plugin file:function wpdocs_flush_rewrite_rules() { flush_rewrite_rules(); } add_action( 'init', 'wpdocs_flush_rewrite_rules' );You have now successfully used the
apply_filtersfunction with theauthor_rewrite_rulesfilter to modify the author rewrite rules based on multiple conditions in WordPress. This approach allows you to customize the URL structure for author archives to fit your specific requirements.