如何编写一键式 WordPress 插件更新代码?
我的客户希望我制作一个插件并通过会员订阅系统出售。他们询问如何推出该插件的更新。通常,我让插件在登录 wp-admin 后拦截仪表板查看。然后它检查服务器上是否有更新版本的插件。如果是这样,它会通知用户,以便他们可以通过单击再次下载。问题是他们必须解压缩文件,复制它们,然后重新保存插件设置。
有没有办法在插件需要更新时编写一键更新代码?这样,就消除了下载插件、解压缩、复制旧文件以及重新运行插件保存的管理面板的其他步骤。
My client wants me to make a plugin and sell it through a membership subscription system. They asked how they can roll out updates for this plugin. Typically I have had the plugin intercept the dashboard viewing after a login into wp-admin. It then checks if a newer version of the plugin is on the server. If so, it notifies the user so that they can download it again with a click. The problem is that they must unzip the files, copy them over, and resave the plugin settings.
Is there a way to code a one-click update for the plugin when it's due for an update? That way, it eliminates the other steps where they download the plugin, unzip, copy over the old files, and rerun an admin panel save for the plugin.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要执行几个步骤,其中大部分可以轻松地借鉴 WordPress 处理其内部更新的方式。
不要每次加载管理页面时都运行它,编写您自己的 _maybe_update_plugins() 并让它定期检查,可能每天一次左右。
WordPress WP_Upgrader 'wp-admin/includes/class-wp-upgrader.php' 中的类(我相信您需要将其包含在您的插件中)做您想做的事情。查看 wp-admin/update.php 并确保您非常仔细地模拟此处的安全预防措施,因为如果不小心操作,可能会引入巨大的安全漏洞。
一旦您整理好通知并确保该过程安全,只需将新版本插件的 URL 传递到下面的代码“$download_link”即可。
如果这不起作用(我没有时间或动力来测试它),请在 WP_Upgrader 类中进行更多研究,并借用它们的功能来下载和提取文件。
There are a few steps that you'd probably want to take, most of which can be easily borrowed from how wordpress handles it's own internal updates.
Don't run it every time the admin page is loaded, write your own version of _maybe_update_plugins() and have it check on a timed interval, probably only once per day or so.
the WordPress WP_Upgrader class in 'wp-admin/includes/class-wp-upgrader.php' (I believe you will need to include that in your plugin) do what you want. Take a look at wp-admin/update.php and make sure that you emulate the security precautions here very closely as you could introduce huge security holes if not done carefully.
Once you've sorted out your notifications and made sure that the process is secure, it's as easy as passing the url for the new version of your plugin to the code below as "$download_link".
If that doesn't do the trick (I've not the time or motivation to test it), dig around a bit more in the WP_Upgrader class and borrow their functions for downloading and extracting files.