您可以为配置文件页面添加新端点。无需使用htaccess管理任何内容。WordPress会帮你做的。
codex: add_rewrite_endpoint
另一种方法(半解决方案)是创建模板文件并通过
template_include
钩但它不会获取用户名(按原样)。
add_filter( \'template_include\', array($this, \'profile_page\'), 99 );
function profile_page($templates){
if ( is_page( \'profile-page\' ) ) {
$new_template = YOURPLUGIN_DIR .\'templates/profile-page\';
if ( \'\' != $new_template ) {
return $new_template ;
}
}
}
创建一个将permalink设置为profile page的页面,并创建函数和profile page内容。。。
编辑:
这里是一个用于概要文件用户页面的重写完整类,无需创建页面。
class wpse242114{
public function __construct(){
add_action( \'wp_loaded\', array($this,\'_flush_rules\') );
add_filter( \'rewrite_rules_array\', array($this,\'_insert_rewrite_rules\') );
add_filter( \'query_vars\', array($this,\'_insert_query_vars\') );
add_filter( \'template_include\', array($this, \'_get_plugin_template\'), 99 );
}
public function _flush_rules() {
$rules = get_option( \'rewrite_rules\' );
if ( ! isset( $rules[\'(profile)/(\\d*)$\'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
public function _insert_rewrite_rules( $rules ) {
$newrules = array();
$newrules[\'(profile)/(\\d*)$\'] = \'index.php?pagename=$matches[1]&id=$matches[2]\';
return $newrules + $rules;
}
public function _insert_query_vars( $vars ) {
array_push( $vars, \'id\' );
return $vars;
}
public function _get_plugin_template($template){
global $wp_query;
if ( $wp_query->query[\'name\'] == \'profile\') {
$new_template = APIKEYABLE_DIR .\'includes/templates/page-login.php\';
if ( \'\' != $new_template ) {
return $new_template ;
}
}
return $template;
}
}
模板文件示例:
<?php get_header(); ?>
<div class="container">
<div id="main-grid" class="row">
<div id="primary" class="content-area col-md-8">
<main id="main" class="site-main" role="main">
<?php
$userid = get_current_user_id();
$user = new WP_User($userid);
if ( $user->exists() ) {
// do something
}
</main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
</div><!-- .row -->