如果您的插件允许用户提交数据(无论是在管理员端还是公共端),它都应该检查用户功能。WordPress 插件可以检查用户是否具备某些权限,并据此执行特定操作。current_user_can () WordPress 函数可以帮助执行此检查。例如,安全插件可以仅向管理员显示其选项面板,但仍向所有用户显示安全警告。
构建高效安全层最重要的一步是建立用户权限系统。WordPress 以用户角色和权限的形式提供了此功能。
每个登录 WordPress 的用户都会根据其用户角色自动分配特定的用户功能。
用户角色只是表示用户所属组的一种花哨说法。每个组都有一组特定的预定义权限。
例如,您网站的主要用户将拥有管理员的用户角色,而其他用户则可能拥有编辑或作者等角色。您可以将多个用户分配给一个角色,例如,一个网站可能有两个管理员。
用户能力是您分配给每个用户或用户角色的特定权限。
例如,管理员拥有“manage_options”权限,可以查看、编辑和保存网站选项。而编辑者则缺乏此功能,因此无法与选项进行交互。
然后在管理员界面的各个点检查这些功能。根据分配给角色的功能,可能会添加或删除菜单、功能以及 WordPress 体验的其他方面。
在构建插件时,请确保仅当当前用户具有必要的功能时才运行代码。
用户角色越高,用户拥有的功能越多。每个用户角色都会继承层次结构中先前的角色。
例如,“管理员”是单个站点安装中的最高用户角色,它继承了以下角色及其功能:“订阅者”、“贡献者”、“作者”和“编辑”。
下面的示例在前端创建了一个链接,允许删除帖子。由于此代码不检查用户权限,因此它允许任何网站访问者删除帖子!
/** * Generate a Delete link based on the homepage url. * * @param string $content Existing content. * * @return string|null */ 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'] ) && 'wporg_frontend_delete' === $_GET['action'] ) { // 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' );
上面的示例允许任何网站访问者点击“删除”链接并将帖子丢弃。但是,我们希望只有编辑及以上级别的用户才能点击“删除”链接。
为了实现这一点,我们将检查当前用户是否具有edit_others_posts只有编辑者或以上级别用户才具有的能力:
/** * Generate a Delete link based on the homepage url. * * @param string $content Existing content. * * @return string|null */ 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'] ) && 'wporg_frontend_delete' === $_GET['action'] ) { // 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 delete post ability */ add_action('plugins_loaded', 'wporg_add_delete_post_ability'); function wporg_add_delete_post_ability() { 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' ); } }
下面是一个自定义函数示例,可用于检查用户是否可以查看页面或帖子。在上面的代码片段中,我们检查用户是否已登录。然后,我们使用 wp_get_current_user 返回当前用户的 WP_User 对象。之后,我们可以通过$user->roles访问用户角色。
显示基本用户信息
检索和显示当前登录用户信息的一种方法是使用wp_get_current user() 函数。具体来说,此函数检索当前用户对象,然后您可以使用该对象显示其相关属性或执行其他操作。
深入了解 WordPress 用户角色和功能
WordPress 用户角色和权限让您能够控制其他用户在您的网站上可以或不可以执行的操作。您可以使用它们来管理用户操作,例如撰写和编辑文章、创建新页面、审核评论、安装插件、添加新用户等等。