ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
如果您的插件允许用户在管理员或公共方面提交数据,则应检查用户功能。 ##用户角色和功能 创建高效安全层最重要的一步就是拥有一个用户权限系统。 WordPress以用户角色和功能的形式提供。 每个登录WordPress的用户都会根据用户角色自动分配具体的用户功能。 用户角色只是说明用户所属的组的一种奇特的方式。每个组都有一组特定的预定义功能。 例如,您的网站的主要用户将具有管理员的用户角色,而其他用户可能具有编辑器或作者等角色。您可以将多个用户分配到角色,即网站可能有两个管理员。 用户功能是您分配给每个用户或用户角色的特定权限。 例如,管理员具有“manage_options”功能,允许他们查看,编辑和保存网站的选项。另一方面,编辑者缺乏这种能力,这将阻止他们与选项进行交互。 然后在管理员的各个不同点检查这些功能。取决于分配给角色的功能;可以添加或删除WordPress体验的菜单,功能和其他方面。 在构建插件时,请确保仅在当前用户具有必要功能时运行代码。 层次结构 用户角色越高,用户拥有的功能越多。每个用户角色都会继承层次结构中的以前的角色。 例如,单个站点安装中最高用户角色的“管理员”继承了以下角色及其功能:“订阅者”,“贡献者”,“作者”和“编辑者”。 ## 无限制 以下示例创建了一个前端的链接,该链接能够删除帖子。由于此代码不检查用户功能,它允许任何访问者的站点垃圾邮件! ``` <?php /** * generate a Delete link based on the homepage url */ function wporg_generate_delete_link($content) { // run only for single post page if (is_single() && in_the_loop() && is_main_query()) { // add query arguments: action, post $url = add_query_arg( [ 'action' => 'wporg_frontend_delete', 'post' => get_the_ID(), ], home_url() ); return $content . ' <a href="' . esc_url($url) . '">' . esc_html__('Delete Post', 'wporg') . '</a>'; } return null; } /** * request handler */ function wporg_delete_post() { if (isset($_GET['action']) && $_GET['action'] === 'wporg_frontend_delete') { // verify we have a post id $post_id = (isset($_GET['post'])) ? ($_GET['post']) : (null); // verify there is a post with such a number $post = get_post((int)$post_id); if (empty($post)) { return; } // delete the post wp_trash_post($post_id); // redirect to admin page $redirect = admin_url('edit.php'); wp_safe_redirect($redirect); // we are done die; } } /** * add the delete link to the end of the post content */ add_filter('the_content', 'wporg_generate_delete_link'); /** * register our request handler with the init hook */ add_action('init', 'wporg_delete_post'); ``` ## 限于具体能力 上面的示例允许网站的任何访问者点击“删除”链接并垃圾邮件。 但是,我们只希望Editors及以上版本能够点击“删除”链接。 为了实现这一点,我们将检查当前用户是否具有edit_others_posts功能,只有Editors或以上版本具有: ``` <?php /** * generate a Delete link based on the homepage url */ function wporg_generate_delete_link($content) { // run only for single post page if (is_single() && in_the_loop() && is_main_query()) { // add query arguments: action, post $url = add_query_arg( [ 'action' => 'wporg_frontend_delete', 'post' => get_the_ID(), ], home_url() ); return $content . ' <a href="' . esc_url($url) . '">' . esc_html__('Delete Post', 'wporg') . '</a>'; } return null; } /** * request handler */ function wporg_delete_post() { if (isset($_GET['action']) && $_GET['action'] === 'wporg_frontend_delete') { // verify we have a post id $post_id = (isset($_GET['post'])) ? ($_GET['post']) : (null); // verify there is a post with such a number $post = get_post((int)$post_id); if (empty($post)) { return; } // delete the post wp_trash_post($post_id); // redirect to admin page $redirect = admin_url('edit.php'); wp_safe_redirect($redirect); // we are done die; } } if (current_user_can('edit_others_posts')) { /** * add the delete link to the end of the post content */ add_filter('the_content', 'wporg_generate_delete_link'); /** * register our request handler with the init hook */ add_action('init', 'wporg_delete_post'); } ```