对于一个开放注册的多用户WordPress站点,默认情况下,有文章编辑权限的用户是可以更新和删除自己的文章的,如果我们要限制用户只在文章发布后的指定的时间段(比如30天)内才可以更新和删除文章,该如何实现呢?

将下面的代码添加到当前主题的 functions.php 文件中:
function wpbeginner_restrict_editing( $allcaps, $cap, $args ) {
// Bail out if we're not asking to edit or delete a post ...
if( 'edit_post' != $args[0] && 'delete_post' != $args[0]
// ... or user is admin
|| !empty( $allcaps['manage_options'] )
// ... or user already cannot edit the post
|| empty( $allcaps['edit_posts'] ) )
return $allcaps;
// Load the post data:
$post = get_post( $args[2] );
// Bail out if the post isn't published:
if( 'publish' != $post->post_status )
return $allcaps;
//如果文章超过30天。请根据自己的需要修改
if( strtotime( $post->post_date ) < strtotime( '-30 day' ) ) {
//Then disallow editing.
$allcaps[$cap[0]] = FALSE;
}
return $allcaps;
}
add_filter( 'user_has_cap', 'wpbeginner_restrict_editing', 10, 3 ); |
function wpbeginner_restrict_editing( $allcaps, $cap, $args ) {
// Bail out if we're not asking to edit or delete a post ...
if( 'edit_post' != $args[0] && 'delete_post' != $args[0]
// ... or user is admin
|| !empty( $allcaps['manage_options'] )
// ... or user already cannot edit the post
|| empty( $allcaps['edit_posts'] ) )
return $allcaps;
// Load the post data:
$post = get_post( $args[2] );
// Bail out if the post isn't published:
if( 'publish' != $post->post_status )
return $allcaps;
//如果文章超过30天。请根据自己的需要修改
if( strtotime( $post->post_date ) < strtotime( '-30 day' ) ) {
//Then disallow editing.
$allcaps[$cap[0]] = FALSE;
}
return $allcaps;
}
add_filter( 'user_has_cap', 'wpbeginner_restrict_editing', 10, 3 );
上面的函数会检测用户是否有编辑和删除文章的能力,之后检测文章的发布状态,30天内,用户还可以编辑或删除该文章,如果超过30天,用户就没办法再更新或删除这篇文章(超级管理员还是可以编辑或删除所有文章的)。
参考资料:http://www.wpbeginner.com/wp-tutorials/how-to-block-wordpress-post-updates-and-deletion-after-a-set-period/
来源:
https://www.wpdaxue.com/block-wordpress-post-updates-and-deletion.html
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_33265.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57