显示所有注册用户(不仅是作者)的简单公共用户配置文件

在一个简单的WordPress插件中, 我使用以下代码将评论者链接重定向到URL / user / user_id:

define('PROFILE', '< a href="http://www.srcmini.com/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');

我该如何显示一个简单的公共用户个人资料(针对所有注册用户, 而不仅仅是作者), 显示该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(); ?>

但是, 当重新请求URL / user / user_id时, 如何使WordPress执行该PHP脚本?
我应该在这里使用Rewrite API以及如何使用?我尝试了以下方法, 但没有看到任何效果:
add_rewrite_rule('/user/(\d+)', 'index.php?XXX=$matches[1]', 'top' ); flush_rewrite_rules();

还是我错过了一些东西, 例如也许我应该先添加自定义帖子类型? (如你所见-我感到困惑和” 迷失在文档中” )。
更新:
我是否可以覆盖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');

#1 以下是一些(希望有所帮助)的说明:
避免将所有内容放到庞大的超级类中, 让你的类尽可能的小而集中(面向任务)。自动加载类也可以派上用场。
将add_rewrite_rule保留在init钩子中, 因为当你访问永久链接设置页面并重新保存永久链接时, 你不想放弃重写:public static function init(){// … 你的代码… add_rewrite_rule (‘ user /([[^ /] *)/?’ , ‘ index.php?wpcg_user = $ matches [1]’ , ‘ top’ ); }注意/?部分, 以说明用户/ 123和用户/ 123 /。 add_rewrite_rule()调用不会自动刷新重写规则。
另一种方法是使用add_rewrite_endpoint注册端点:public static function init(){// … 你的代码… add_rewrite_endpoint(‘ user’ , EP_ALL); }, 你可以在其中修改可访问的位置:EP_ALL, EP_PAGES, EP_PERMALINK等。请注意, 它将创建一个新的重写规则, 其中/ user / 123 /映射到?user = 123。
你可以使用template_redirect操作或template_include过滤器来修改要显示的模板。
你应该使用query_vars过滤器注册自定义GET变量wpcg_user, 例如:public static function query_vars($ vars){$ vars [] =’ wpcg_user’ ; 返回$ vars; }, 以便稍后使用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’ ))
如果你选择使用相应的钩子, 则template_redirect方法将如下所示:public static function template_redirect(){$ wpcg_user = get_query_var(‘ wpcg_user’ ); $ tpl =’ tpl-user.php’ ; //你的自定义模板文件。 if(!empty($ wpcg_user)){if(” == locate_template($ tpl, TRUE))include(plugin_dir_path(__FILE__)。$ tpl); 出口(); }}, 你可能必须调整tpl-user.php模板的路径, 例如, 如果将其保存在wp-city-gender插件目录中的子目录中。注意, 我们使用locate_template()函数的第二个输入参数, 因此它将在找到模板时加载它。
你的tpl-user.php模板文件可能如下所示:< ?php / *模板名称:Public User Profile * /?> < ?php get_header(); ?> < div> < ?php //获取用户查询:$ wpcg_user = get_query_var(‘ wpcg_user’ ); //通过ID获取相应的用户:$ user = get_user_by(‘ id’ , absint($ wpcg_user)); if($ user):printf(‘ < p> 名称:%s < / p> ’ , $ user-> display_name); printf(‘ < p> City:%s < / p> ’ , get_user_meta($ user-> ID, ‘ city’ , TRUE)); printf(‘ < p> 性别:%s < / p> ’ , get_user_meta($ user-> ID, ‘ gender’ , TRUE)); 否则:_e(‘ 抱歉, 找不到用户!’ ); 万一; ?> < / 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’ )); 激活相应的钩子回调。
有许多工具可以帮助你进行自定义重写, 例如Monkeyman Rewrite Analyzer。它已经有一段时间没有更新了, 但是效果仍然不错。
这是显示/ 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根目录中。
#2 如何检查页面模板而不是URL?
if(is_page_template(‘ public_profile.php’ )){//加载使用public_profile.php模板文件(你的公共用户配置文件模板文件)的页面时执行的代码。 }
看看这个函数is_page_template()
或者, 你也可以获取当前页面使用的模板来检查模板。参见get_page_template()
#3 WordPress会在www.example.com/author/[USER NAME]页面上显示每个(无论是作者, 订户还是其他任何人)用户的详细信息, 因此你必须重定向到该URL-
www.example.com/author/< ?php echo $comment-> comment_author; ?>

并且, 要编辑详细信息页面的格式, 应编辑主题的author.php。

    推荐阅读