Ale*_*ber 4 wordpress wordpress-theming wordpress-plugin
在一个简单的WordPress插件中,我/user/user_id使用以下代码将commenter链接重定向到URL :
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');
Run Code Online (Sandbox Code Playgroud)
我怎么能显示一个简单的公共用户配置文件(对于所有注册用户而不仅仅是作者)在该URL上显示用户名?
我想我应该在我的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(); ?>
Run Code Online (Sandbox Code Playgroud)
但是当重新获得URL时,如何使WordPress执行该PHP脚本/user/user_id?
我应该在这里使用Rewrite API以及如何使用?我尝试了以下,但没有看到任何影响:
add_rewrite_rule('/user/(\d+)',
'index.php?XXX=$matches[1]',
'top'
);
flush_rewrite_rules();
Run Code Online (Sandbox Code Playgroud)
或者我错过了什么,也许我应该首先添加自定义帖子类型?(如你所见 - 我很困惑,"迷失在文档中").
更新:
我可以覆盖the_content挂钩 - 但如何检测/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');
Run Code Online (Sandbox Code Playgroud)
以下是一些(希望是有用的)评论:
避免将所有内容都放入一个巨大的超级类中,尽可能保持您的课程小而专注(面向任务).自动加载类也可以派上用场.
保持你add_rewrite_rule的内init勾,因为你不希望当您访问永久链接设置页面,并重新保存永久链接松动重写:
public static function init()
{
// ... your code ...
add_rewrite_rule(
'user/([^/]*)/?',
'index.php?wpcg_user=$matches[1]',
'top'
);
}
Run Code Online (Sandbox Code Playgroud)
注意/?部分,考虑到user/123和user/123/.该add_rewrite_rule()调用不会自动刷新重写规则.
另一种方法是使用add_rewrite_endpoint以下方式注册端点:
public static function init()
{
// ... your code ...
add_rewrite_endpoint( 'user', EP_ALL );
}
Run Code Online (Sandbox Code Playgroud)
在这里你可以修改的地方,这将是访问:EP_ALL,EP_PAGES,EP_PERMALINK,....请注意,它将创建一个/user/123/映射到的新重写规则?user=123.
您可以使用template_redirect操作或template_include过滤器来修改要显示的模板.
您应该wpcg_user使用query_vars过滤器注册自定义GET变量,例如:
public static function query_vars( $vars )
{
$vars[] = 'wpcg_user';
return $vars;
}
Run Code Online (Sandbox Code Playgroud)
所以你以后可以用它来检索它get_query_var( 'wpcg_user' ).
当get_query_var( 'wpcg_user')非空时,您知道请求的URL就像 http://example.com/user/123/.
您应该允许WordPress管理员tpl-user.php通过将其复制到当前主题目录来覆盖默认插件模板.该功能locate_template可以帮助您:
if ( '' != locate_template( 'tpl-user.php') )
Run Code Online (Sandbox Code Playgroud)template_redirect如果您选择使用相应的钩子,您的方法可能如下所示:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
您可能需要调整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(); ?>
Run Code Online (Sandbox Code Playgroud)
我们在哪里使用该get_user_by()函数,来检索相应的用户数据.
我们可以轻松地使用用户slug而不是用户id,get_user_by( 'slug', $wpcg_user );而不是使用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') );
Run Code Online (Sandbox Code Playgroud)
激活相应的钩子回调.
有许多工具可以帮助您进行自定义重写,例如Monkeyman Rewrite Analyzer.它暂时没有更新,但仍然很好用.
这是一个显示/user/1/检查的屏幕截图,上面的重写:

我希望这有帮助.如果您需要帮助来实现这一点,请告诉我.我在我的安装上测试了这些修改,它在那里工作.
更新:
回应评论:
例如,如何处理模板位于子目录中的情况/templates/profile.php?
我们可以使用以下内容:
if ( '' == locate_template( 'templates/profile.php', TRUE ) )
include( plugin_dir_path( __FILE__ ) . 'templates/profile.php' );
Run Code Online (Sandbox Code Playgroud)
这意味着:
如果/wp-content/themes/MYTHEME/templates/profile.php找到,则会自动加载.
否则:/wp-content/plugins/wp-city-gender/templates/profile.php已加载.
我们假设它__FILE__ 在wp-city-gender根目录中.