在 a simple WordPress plugin我已将评论者链接重定向到 URL /user/user_id
使用以下代码:
define('PROFILE', '<a href="/user/%d">%s</a>');
function my_get_comment_author_link($comment_ID) {
global $comment;
return sprintf(PROFILE, $comment->user_id,
$comment->comment_author);
}
add_action('get_comment_author_link',
'my_get_comment_author_link');
我怎样才能显示一个简单的公共(public)用户配置文件(适用于所有注册用户,而不仅仅是作者)在该 URL 上显示用户名?
我想我应该在我的 child 2013 主题中创建一个 PHP 脚本 -
<?php
/* Template Name: Public User Profile */
?>
<?php get_header(); ?>
<?php
$my_query = new WP_Query($args);
// somehow get the user ID out of URL?
// somehow get the WP_User object?
printf('<p>Name: %s</p>', $user->display_name);
?>
<?php get_footer(); ?>
但是如何让 WordPress 执行那个 PHP 脚本,当 URL
/user/user_id
有要求吗?我应该使用 Rewrite API这里和如何?我尝试了以下方法,但没有看到任何效果:
add_rewrite_rule('/user/(\d+)',
'index.php?XXX=$matches[1]',
'top'
);
flush_rewrite_rules();
或者我错过了什么,比如我应该先添加一个自定义帖子类型? (如您所见 - 我很困惑并且“迷失在文档中”)。
更新:
我可以覆盖
the_content
钩子(Hook) - 但是如何检测到 /user/user_id
页面已被调用?function my_content($content)
{
if (NOT /user/user_id CALLED)
return $content;
// otherwise somehow extract the user_id
// somehow get the corresponding WP_User
return sprintf('<p>Name: %s</p>', $user->display_name);
}
add_action('the_content', 'my_content');
最佳答案
以下是一些(希望有帮助的)评论:
add_rewrite_rule
在 init
内钩子(Hook),因为您不想在访问永久链接设置页面并重新保存永久链接时丢失重写:public static function init()
{
// ... your code ...
add_rewrite_rule(
'user/([^/]*)/?',
'index.php?wpcg_user=$matches[1]',
'top'
);
}
注意
/?
部分,同时考虑 user/123
和 user/123/
. add_rewrite_rule()
call 不会自动刷新重写规则。 add_rewrite_endpoint
注册端点:public static function init()
{
// ... your code ...
add_rewrite_endpoint( 'user', EP_ALL );
}
您可以在其中修改可访问的位置:
EP_ALL
, EP_PAGES
, EP_PERMALINK
, ...
.请注意,它将创建一个新的重写规则,其中 /user/123/
映射到 ?user=123
. template_redirect
行动或 template_include
过滤以修改您要显示的模板。 wpcg_user
, 与 query_vars
过滤器,例如:public static function query_vars( $vars )
{
$vars[] = 'wpcg_user';
return $vars;
}
因此您可以稍后使用
get_query_var( 'wpcg_user' )
检索它. get_query_var( 'wpcg_user')
是非空的,你知道请求的 url 就像 http://example.com/user/123/
. tpl-user.php
, 通过将其复制到当前主题目录中。函数 locate_template
可以帮助您:if ( '' != locate_template( 'tpl-user.php') )
template_redirect
方法,如果您选择使用相应的钩子(Hook),则可能如下所示:public static function template_redirect()
{
$wpcg_user = get_query_var( 'wpcg_user' );
$tpl = 'tpl-user.php'; // Your custom template file.
if( ! empty( $wpcg_user ) )
{
if ( '' == locate_template( $tpl, TRUE ) )
include( plugin_dir_path( __FILE__ ) . $tpl );
exit();
}
}
您可能需要调整
tpl-user.php
的路径模板,例如,如果您将其保存在 wp-city-gender
的子目录中插件目录。请注意,我们使用 locate_template()
的第二个输入参数。函数,所以它会加载模板,如果它可以找到它。 tpl-user.php
模板文件可能如下所示:<?php
/* Template Name: Public User Profile */
?>
<?php get_header(); ?>
<div>
<?php
// Get the user query:
$wpcg_user = get_query_var( 'wpcg_user' );
// Get corresponding user by id:
$user = get_user_by( 'id', absint( $wpcg_user ) );
if( $user ):
printf('<p> Name: %s </p>', $user->display_name );
printf('<p> City: %s </p>', get_user_meta( $user->ID, 'city', TRUE));
printf('<p> Gender: %s </p>', get_user_meta( $user->ID, 'gender', TRUE));
else:
_e( 'Sorry, no user found!' );
endif;
?>
</div>
<?php get_footer(); ?>
我们在哪里使用
get_user_by()
函数,检索相应的用户数据。 get_user_by( 'slug', $wpcg_user );
,我们可以很容易地使用用户 slug 而不是用户 ID。而不是 get_user_by( 'id', absint( $wpcg_user ) );
. add_action( 'template_redirect', array( CNAME, 'template_redirect') );
add_filter( 'query_vars', array( CNAME, 'query_vars') );
//add_action( 'template_include', array( CNAME, 'template_include') );
激活相应的钩子(Hook)回调。
这是显示
/user/1/
的屏幕截图检查,上面重写:我希望这有帮助。如果您需要帮助来实现这一点,请告诉我。我在我的安装中测试了这些修改并且它在那里工作。
更新:
回应评论:
如何处理模板位于子目录的情况,例如
/templates/profile.php
?我们可以使用以下内容:
if ( '' == locate_template( 'templates/profile.php', TRUE ) )
include( plugin_dir_path( __FILE__ ) . 'templates/profile.php' );
这表示:
/wp-content/themes/MYTHEME/templates/profile.php
定位,然后会自动加载。 /wp-content/plugins/wp-city-gender/templates/profile.php
已加载。 我们假设
__FILE__
在wp-city-gender
根目录。
关于wordpress - 为所有注册用户(不仅仅是作者)显示简单的公共(public)用户配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23827939/