如何防止 WordPress 页面被用户删除 [英] How to prevent a WordPress page from being deleted by a user

查看:36
本文介绍了如何防止 WordPress 页面被用户删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些 WordPress 插件会创建一个页面或帖子,插件的正常功能依赖于该页面或帖子的存在.例如,管理电子邮件列表的插件可能依赖于取消订阅页面,用户可能会有意或无意地删除该页面.

Some WordPress plugins create a page or a post, and proper function of the plugin relies on the existence of that page or post. For example, a plugin that manages an e-mail list might rely on an unsubscribe page, which a user might delete on purpose or by accident.

插件如何防止其页面被删除?

How can a plugin prevent its page from being deleted?

推荐答案

首先,当您创建页面时,您可以将其 ID 存储在您以后需要获取的选项中:

First, when you create the page, you can store its ID in an option that you need to get later:

add_option('undeleteable_page_id', $the_page_id, '', 'no'); // 'no' so this option does not load on every page

然后,您使用函数挂钩删除操作以防止删除:

Then, you hook into the delete actions with functions to prevent the deletion:

add_action('deleted_post', 'prevent_undeleteable_page_deletion');
add_action('trashed_post', 'prevent_undeleteable_page_trash');

在这些函数中,您检查被删除页面的 id,并将其与创建页面时存储的 id 进行比较.

In those functions, you check the id of the page being deleted and compare it to the id that you stored when you created the page.

if($id == get_option('undeleteable_page_id')) ...

在防止垃圾"功能中,您将状态更改回发布.在防止删除功能中,您重新创建页面.

In the "prevent trash" function, you change the status back to publish. In the prevent deletion function, you re-create the page.

这就是我这样做的方式,它对我有用.我很想看看其他人是如何解决这个问题的.

This is how I did this, and it worked for me. I would love to see how others may have approached this problem.

这篇关于如何防止 WordPress 页面被用户删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆