WordPress 限定用户更新或删除文章的时间期限

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

block-wordpress-post-updates-and-deletion-wpdaxue

将下面的代码添加到当前主题的 functions.php 文件中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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/

本人擅长Ai、Fw、Fl、Br、Ae、Pr、Id、Ps等软件的安装与卸载,精通CSS、JavaScript、PHP、ASP、C、C++、C#、Java、Ruby、Perl、Lisp、Python、Objective-C、ActionScript、Pascal等单词的拼写,熟悉Windows、Linux、OS X、Android、iOS、WP8等系统的开关机。

通过下面的方式来联系我们:

电邮:138762189@qq.com

联系QQ:点击这里给我发消息

官方站:www.tadke.com

※ ※ 联系请加我的企鹅号 ※※

※ ※技术支持请微信联系站长 ※※

Copyright © 2023 Tadke.com. 琼ICP备20000547号