删除wordpress_在设定时间后如何阻止WordPress发布更新和删除

删除wordpress

Managing WordPress website with multiple authors can be complicated sometimes. There are plugins to manage editorial workflow, but you may come across situations where you need specific solutions for better control of your WordPress site. Recently, we helped a user find such a solution. They wanted to block WordPress post updates and deletion after a set period of time for all users (including editors) after a set period of time. For example, if a published post is 30 days or older, then it cannot be edited or deleted by editors. Only administrators can modify that post. In this article, we will show you how to block post edit, updates, and deletion after a set period of time in WordPress.

由多个作者管理WordPress网站有时会很复杂。 有一些插件可以管理编辑工作流程 ,但是您可能会遇到需要特定解决方案以更好地控制WordPress网站的情况。 最近,我们帮助用户找到了这样的解决方案。 他们希望在设定的时间段之后阻止所有用户(包括编辑器)在WordPress上的帖子更新和删除。 例如,如果已发布的帖子已超过30天,则编辑者将无法对其进行编辑或删除。 只有管​​理员可以修改该帖子。 在本文中,我们将向您展示如何在WordPress设置的一段时间后阻止帖子的编辑,更新和删除。

Locking posts updates and deletion for older published posts in WordPress

All you need to do is add the following code in your theme’s functions.php file or in a site-specific plugin.

您所需要做的就是在主题的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;

    //if post is older than 30 days. Change it to meet your needs
    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 );


This function checks if the user has the capability to edit or delete posts. After that it checks for the post status. If a post is published and is older than 30 days, then user’s capability to edit and delete the post is taken away. If a post is published, but it is not older than 30 days, then the users with the ability to edit posts can still edit it. Note: Administrators can edit and delete posts anytime they want.

此功能检查用户是否具有编辑或删除帖子的能力。 之后,它将检查发布状态。 如果发布的帖子已超过30天,则用户编辑和删除该帖子的能力将被取消。 如果发布了帖子,但发布日期不超过30天,则具有编辑帖子功能的用户仍可以对其进行编辑。 注意:管理员可以根据需要随时编辑和删除帖子。

We hope that this article helped anyone who is looking to block post edit, update and deletion in WordPress after a set period of time. Would you ever do this on your site? What use cases can you see for something like this? Let us know in the comments below.

我们希望本文对在一段时间后希望阻止WordPress中的帖子编辑,更新和删除的所有人有所帮助。 您会在您的网站上这样做吗? 您会看到什么样的用例? 在下面的评论中让我们知道。

Source:Smhmic

资源: Smhmic

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-block-wordpress-post-updates-and-deletion-after-a-set-period/

删除wordpress

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值