如果您的插件允许用户提交数据(无论是在管理员端还是公共端),它应该检查用户功能。
用户角色和功能
创建高效安全层的最重要步骤是建立用户权限系统。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' );
}
}