简体   繁体   English

我应该在哪里存储我的多站点WordPress插件的全局数据?

[英]Where should I store global data for my multi-site WordPress plugin?

I wrote a plugin for WordPress which has a few user-configurable settings that are stored using WordPress's suggested method. 我为WordPress编写了一个插件,它有一些用户可配置的设置,使用WordPress建议的方法存储。 I know they are saved in the wp_options table, but that is abstracted by the WordPress options API. 我知道它们保存在wp_options表中,但是它被WordPress选项API抽象出来。

Now I'm trying to add a "global override" of the settings that can be configured in the Network Admin section of a multi-site installation. 现在,我正在尝试添加可以在多站点安装的“网络管理员”部分中配置的设置的“全局覆盖”。 I found the appropriate hooks to design my settings page, however I can't find any info about where to save the data. 我找到了适当的钩子来设计我的设置页面,但是我找不到有关保存数据的位置的任何信息。

If I save it using the normal options API, then the settings get saved individually for each site. 如果我使用普通选项API保存它,则会为每个站点单独保存设置。 I'm looking for a place to save them globally for all sites, so the plugin can first look to see if the settings have been globally overridden by the server admin. 我正在寻找一个在全球范围内为所有站点保存它们的地方,因此该插件可以首先查看设置是否已被服务器管理员全局覆盖。

I can just write some code to write directly to the wp_options table of one of the sites (for example site #1) or even create my own table. 我可以编写一些代码直接写入其中一个站点(例如站点#1)的wp_options表,甚至可以创建自己的表。 I know how to do all of these things, but I don't want to do that if there's a preferred way to write mult-site plugins. 我知道如何做所有这些事情,但如果有一种编写多站点插件的首选方法,我不想这样做。

Thanks for any advice. 谢谢你的建议。

As you know the main deference between developing a single WordPress plugin vs. a multisite plugin is where data is written. 如您所知,开发单个WordPress插件与多站点插件之间的主要区别在于编写数据。 According to this article since plugins normally have complete reign over one database, and in some cases the plugins local pages files you must be sure to only call the $wpdb global object instead of a hard-coded reference to a table. 根据这篇文章,因为插件通常在一个数据库上完全统治,并且在某些情况下插件本地页面文件必须确保只调用$ wpdb全局对象而不是对表的硬编码引用。

For example, if you need to execute an SQL command on the posts table, then do not use wp_posts as the table name $post_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM wp_posts")); 例如,如果需要在posts表上执行SQL命令,则不要使用wp_posts作为表名$post_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM wp_posts"));

use this 用这个

$post_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->posts"));

Please see the article for further info. 有关详细信息,请参阅文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM