wordpress插件_什么是WordPress MU插件?

wordpress插件

WordPress core is full of features, however, sometimes it is not enough, and we need more. This can be achieved thanks to plugins, and sometimes themes can even add features. However, how can we decide whether a feature should be a plugin or a theme?

WordPress核心具有很多功能,但是,有时它还不够,我们还需要更多。 这可以通过插件实现,有时主题甚至可以添加功能。 但是,我们如何确定某个功能应该是插件还是主题?

The answer to this question can be tricky: it is not always straightforward. If a feature must appear in the theme, or, on the contrary, if it can stay unused without breaking the theme, then you easily know where you should implement it.

这个问题的答案可能很棘手:它并不总是那么简单。 如果某个功能必须出现在主题中,或者相反,如果它可以在不破坏主题的情况下保持不使用状态,那么您很容易知道应该在哪里实现它。

What if a feature must be there all the time? Independent of your current theme, this feature might be so important that it must not be deactivated and it does not have its place in the classic plugins system. For these special features, Must-Use plugins can be used.

如果功能必须一直存在怎么办? 与您当前的主题无关,此功能可能非常重要,以致不能禁用它,并且在经典插件系统中也没有它的位置。 对于这些特殊功能,可以使用必备插件

什么是必须使用的插件? (What Is a Must-Use Plugin?)

A must-use plugin (also called a ‘mu-plugin’) is a plugin that will always be activated by default, without you needing to do it yourself. To activate a mu-plugin, you just have to upload it in the right directory, and WordPress will automatically know that this plugin must be used. Moreover, a mu-plugin cannot be deactivated: it will always be executed, unless it is uninstalled.

必须使用的插件 (也称为“ mu-plugin”)是默认情况下始终会被激活的插件,无需您自己进行操作。 要激活mu插件,只需将其上传到正确的目录中,WordPress将自动知道必须使用此插件。 而且, 不能禁用mu插件 :它将一直执行,除非将其卸载。

Originally, mu-plugins were only available for WPMU, for those sites which used multiple (multisite) blogs. Back then, a mu-plugin was just a plugin activated for all blogs.

最初,mu插件仅适用于WPMU,适用于使用多个(多站点)博客的站点。 当时,mu插件只是为所有博客激活的插件。

Now, since the version 2.8, this feature is in the main branch of WordPress and the ‘MU’ part in mu-plugins has a new meaning: from ‘multi-user’, it became ‘must-use’ and the use of this feature has been changed.

现在,从2.8版开始,此功能在WordPress的主要分支中,并且mu-plugins中的“ MU”部分具有新的含义:从“多用户”开始,它变成了“必须使用”,并且对此进行了使用功能已更改。

The question is not ‘must this feature appear on all my blogs?‘ anymore, but rather ‘is this feature so important that it can be considered as an extension of WordPress?‘.

问题不是' 此功能是否必须出现在我所有的博客上? '不再是,而是' 该功能是否如此重要,可以将其视为WordPress的扩展? '。

Must-use plugins allow users to add features which are not present by default in the WordPress core but that are needed for users.

必须使用的插件允许用户添加WordPress核心默认情况下不存在但用户需要的功能。

Must-use plugins can also be seen as features that do not have their place in a theme or classic plugin. For example, as we covered in a previous SitePoint article on updating WordPress, plugins and themes, we can enable automatic updates for plugins and themes, thanks to some specific filters. Adding these filters into another plugin does not really make sense, and even less into a theme. Enabling automatic updates is a perfect example of features for which mu-plugins are useful.

必须使用的插件也可以被视为在主题插件或经典插件中没有位置的功能。 例如,如我们先前在SitePoint上有关更新WordPress,插件和主题的文章中所述,由于某些特定的过滤器,我们可以启用插件和主题的自动更新。 将这些过滤器添加到另一个插件中实际上没有任何意义,甚至没有一个主题。 启用自动更新是mu插件有用的功能的完美示例。

必备插件的功能是什么? (What Are the Features of a Must-Use Plugin?)

The main feature of a mu-plugin is that it is activated by default. All you have to do is upload the file into the mu-plugins directory (we will see where to find this below). Then, the plugin is automatically activated, you don’t have to log in to do this yourself: once the file is in the right place, WordPress will always execute it.

mu插件的主要功能是默认情况下已激活。 您所要做的就是将文件上传到mu-plugins目录中(我们将在下面找到该文件的位置)。 然后,该插件会自动激活,您无需亲自登录即可:文件放置在正确的位置后,WordPress将始终执行该文件。

Moreover, a mu-plugin can’t be deactivated in the Plugins page of your WordPress dashboard: the only way to deactivate a mu-plugin is by deleting or moving its file. That way, no one can accidentally deactivate a needed feature.

此外,不能在WordPress仪表板的“ Plugins页面中停用mu插件:停用mu插件的唯一方法是删除或移动其文件。 这样,没有人可以意外停用所需的功能。

Because they are not activated, mu-plugins can not use activation hooks which are usually used by classic plugins to initialize some needed things like options in the database. The immediate consequence is that if you need activation hooks, you won’t be able to use mu-plugins for the feature you wanted.

因为它们未激活,所以mu插件不能使用激活钩子,经典钩子通常使用它们来初始化一些必需的东西,例如数据库中的选项。 直接的后果是,如果您需要激活挂钩,则将无法使用mu插件获得所需的功能。

Must-use plugins are loaded before classic plugins, in alphabetical order. It can be useful to know if your mu-plugin is there to load a library used in all the pages of your website for example: if a classic plugin uses this library, you can be sure that it is already loaded because mu-plugins are loaded before and they cannot be deactivated.

必须使用的插件按字母顺序在经典插件之前加载。 例如,知道是否有mu插件可以加载网站所有页面中使用的库可能会很有用:如果经典插件使用了该库,则可以确保已经加载了该插件,因为mu插件已被加载之前已加载,因此无法将其停用。

如何创建必须使用的插件? (How to Create a Must-Use Plugin?)

As you will see below, a must-use plugin is not too different to a classic plugin, but there are some things to know. The goal of this section is to learn how to create a mu-plugin, where to place it and how to see which mu-plugins are currently used by your site.

正如您将在下面看到的,必须使用的插件与经典插件并没有太大的区别,但是有些事情要知道。 本部分的目的是学习如何创建mu插件,将其放置在何处以及如何查看您的站点当前使用了哪些mu插件。

必备插件目录 (The Must-Use Plugins Directory)

A mu-plugin is similar to a classic plugin, except that it is not stored in the plugins directory of the wp-content folder, but rather in the mu-plugins directory, of the same folder. If you have never used mu-plugins before, this directory should not exist, but you can create it without any problem.

mu插件与经典插件类似,不同之处在于它不存储在wp-content文件夹的plugins目录中,而是存储在同一文件夹的mu-plugins目录中。 如果您以前从未使用过mu-plugins,则该目录应该不存在,但是您可以毫无问题地创建它。

Note that the directory where you will store your mu-plugins is /wp-content/mu-plugins, but you can change this by defining two constants in the wp-config.php file: WPMU_PLUGIN_DIR and WPMU_PLUGIN_URL.

请注意,用于存储mu-plugins的目录是/wp-content/mu-plugins ,但是您可以通过在wp-config.php文件中定义两个常量来更改此目录: WPMU_PLUGIN_DIRWPMU_PLUGIN_URL

define('WPMU_PLUGIN_DIR', '/full/path/to/the/new/directory');
define('WPMU_PLUGIN_URL', 'http://URL/to/the/new/directory');

Be careful: if you define these two constants, WordPress won’t redefine them, so if the path is not valid, mu-plugins won’t be loaded. Moreover, in the wp-config.php file, you must define these constants before the line where WordPress includes the wp-settings.php file, that means you won’t be able to use some useful constants like WP_CONTENT_DIR for example.

请注意:如果定义这两个常量,WordPress将不会重新定义它们,因此,如果路径无效,则不会加载mu-plugins。 此外,在wp-config.php文件中,必须在WordPress包含wp-settings.php文件的行之前定义这些常量,这意味着您将无法使用一些有用的常量,例如WP_CONTENT_DIR

Unless you are sure of your directory, you should keep the default values for WPMU_PLUGIN_DIR and WPMU_PLUGIN_URL, as it is safer and more practical.

除非您确定目录,否则应保留WPMU_PLUGIN_DIRWPMU_PLUGIN_URL的默认值,因为它更安全,更实用。

我们的第一个必备插件 (Our First Must-Use Plugin)

Basically, a mu-plugin is a PHP file, so is a classic plugin. We will now create a very basic mu-plugin. It will display a ‘Hello World!’ message, so it will be fairly trivial, but that way we will be able to see if it works right after its creation.

基本上,mu插件是一个PHP文件,经典插件也是。 现在,我们将创建一个非常基本的mu插件。 它将显示“ Hello World!” 消息,因此它相当琐碎,但是通过这种方式,我们将能够查看它在创建后是否立即起作用。

Create a new file in the /wp-content/mu-plugins directory (or another one if you changed it). You can give it any name you want, but hello-world.php seems to be a good idea. Fill it with the following function.

/wp-content/mu-plugins目录中创建一个新文件(如果已更改,则创建另一个文件)。 您可以给它起任何想要的名字,但是hello-world.php似乎是个好主意。 用以下功能填充它。

function displayHelloWorld() {
	echo '<p style="position: absolute; top: 150px; right: 0; padding: 10px; background-color: #0096FF; color: #FFFFFF;">Hello World!</p>';
}

This function will display a paragraph containing the text ‘Hello World!’ and place it at the top right corner of the screen: that way, we can easily see it. Now, you have to call this function.

此功能将显示一个包含文本“ Hello World!”的段落。 并将其放在屏幕的右上角:这样,我们可以轻松看到它。 现在,您必须调用此函数。

It is a test, but it is not an excuse to be dirty: we can call our function via an action, wp_footer for example, which is triggered when the footer is displayed on the blog. Add the following line after the above function.

这是一个测试,但不是变脏的借口:我们可以通过一个动作(例如wp_footer来调用函数,该动作在页脚显示在博客上时触发。 在上述功能之后添加以下行。

add_action('wp_footer', 'displayHelloWorld');

Now, visit your blog. If you haven’t changed anything to the above code, you should see, at the top right corner, a blue box displaying the text ‘Hello World!’. If not, make sure your file is in the right folder: as mu-plugins are automatically activated, you should not have to do anything more than place the file into the right directory.

现在,访问您的博客。 如果您未对上述代码进行任何更改,则应该在右上角看到一个蓝色框,其中显示文本“ Hello World!”。 如果没有,请确保您的文件位于正确的文件夹中:由于mu-plugins被自动激活,因此除了将文件放入正确的目录外,您无需执行其他任何操作。

Hello World WordPress dashboard

查看已安装的必备插件 (Seeing the Installed Must-Use Plugins)

As mu-plugins cannot be deactivated, they won’t be shown into the default list where you can see other plugins. However, it doesn’t mean you can’t see the list of the currently installed must-use plugins.

由于mu插件无法停用,因此它们不会显示在默认列表中,您可以在其中看到其他插件。 但是,这并不意味着您看不到当前安装的必须使用的插件的列表。

In the Plugins section of your WordPress administration, go to the Installed Plugins page. At first, nothing has changed, but look at the tabs below the title of the page. See it?

在WordPress管理的“ Plugins部分中,转到“ Installed Plugins页面。 最初,什么都没有改变,但是请查看页面标题下方的选项卡。 看见?

A new tab appeared: Must-Use. Click on it and, as you can guess, you will see the list of installed mu-plugins, with a message reminding us that this plugins are automatically activated.

出现一个新选项卡: Must-Use 。 单击它,您将可以看到已安装的mu-plugins的列表,并有一条消息提醒我们该插件已自动激活。

Screen of must-use plugins list

As you can see, this list seems to be less complete than the one which shows the classic plugins: the only thing you see is the name of the file, you don’t see what the plugin does.

如您所见,此列表似乎不如显示经典插件的列表完整:您看到的唯一是文件名,您看不到插件的作用。

We can change that, and the way to make this change is the same as for the classic plugins: we will add a comment containing all the information we want. Add the following comment at the beginning of the file.

我们可以更改它,并且进行更改的方式与经典插件相同:我们将添加包含所需所有信息的注释。 在文件开头添加以下注释。

/*
Plugin Name: Hello World!
Description: This is just a test.
Author: Jérémy Heleine
Version: 1.0
Author URI: http://jeremyheleine.me
*/

Replace the different values with yours, refresh the must-use plugins list, and enjoy the result!

用您的替换不同的值,刷新必须使用的插件列表,然后享受效果!

Screenshot of Must-Use Plugins enhanced lIst

子目录 (Subdirectories)

Contrary to a classic plugin, a mu-plugin can’t be stored in a subdirectory of the mu-plugins one so, by default, you won’t be able to sort your mu-plugins with folders. However, if WordPress does not look for plugins in subdirectories, you can do it yourself, with your own loader.

与经典插件相反,mu-plugin不能存储在mu-plugins一个子目录中,因此,默认情况下,您将无法使用文件夹对mu-plugins进行排序。 但是,如果WordPress不在子目录中查找插件,则可以使用自己的加载器自行完成。

This loader will be a new mu-plugin, consisting in only one file, directly stored in the /wp-content/mu-plugins directory. This is necessary because if you store this loader in a subdirectory, it won’t be executed by WordPress and the other plugins won’t be either.

该加载程序将是一个新的mu插件,仅包含一个文件,直接存储在/wp-content/mu-plugins目录中。 这是必要的,因为如果将此加载程序存储在子目录中,则WordPress不会执行该加载程序,其他插件也不会。

As we did above, create a new file in the /wp-content/mu-plugins directory. I chose to name this file load.php but you can choose whatever you want as a name. Then, put the following line in this file.

像上面一样,在/wp-content/mu-plugins目录中创建一个新文件。 我选择将此文件load.php但您可以选择任何名称。 然后,将以下行放入此文件中。

require(WPMU_PLUGIN_DIR . '/myplugin/myplugin.php');

This line includes the myplugin.php file stored in the myplugin subdirectory of the directory where we find must-use plugins which is /wp-content/mu-plugins by default. You have what we wanted: your plugin is executed, even if it is not directly stored in the main directory.

该行包括存储在目录的myplugin子目录中的myplugin.php文件,在该目录中,我们找到了必须使用/wp-content/mu-plugins ,默认情况下该插件为/wp-content/mu-plugins 。 您有了我们想要的东西:即使您的插件没有直接存储在主目录中,也可以执行。

If you add a new folder containing another mu-plugin, you will have to edit the load.php file. However, you can automate this process thanks to some PHP functions. Replace the content of the load.php file by the following lines.

如果添加包含另一个mu插件的新文件夹,则必须编辑load.php文件。 但是,由于有一些PHP函数,您可以使此过程自动化。 用以下几行替换load.php文件的内容。

// Opens the must-use plugins directory
$wpmu_plugin_dir = opendir(WPMU_PLUGIN_DIR);

// Lists all the entries in this directory
while (false !== ($entry = readdir($wpmu_plugin_dir))) {
	$path = WPMU_PLUGIN_DIR . '/' . $entry;

	// Is the current entry a subdirectory?
	if ($entry != '.' && $entry != '..' && is_dir($path)) {
		// Includes the corresponding plugin
		require($path . '/' . $entry . '.php');
	}
}

// Closes the directory
closedir($wpmu_plugin_dir);

This script will automatically include the mu-plugins stored in subdirectories. Note that it requires that the main file of a plugin has the name of its folder.

该脚本将自动包括存储在子目录中的mu插件。 请注意,它要求插件的主文件具有其文件夹的名称。

Moreover, you can see another disadvantage of including mu-plugins stored in subdirectories: as they are not found by WordPress by default, they won’t appear in the must-use plugins list we presented above. That way, you won’t be able to see which must-use plugin is installed without looking at the directory itself.

此外,您还会看到将mu-plugins包含在子目录中的另一个缺点:由于默认情况下WordPress找不到它们,因此它们不会出现在我们上面介绍的必须使用的插件列表中。 这样,如果不查看目录本身,就无法查看安装了哪个必备插件。

结论 (In Conclusion)

Must-use plugins are a way to add features to the WordPress core by adding files into the wp-content directory. The advantage is they won’t be deleted by an update of WordPress, so you can be sure that you will always retrieve your features.

必须使用的插件是通过将文件添加到wp-content目录中来向WordPress核心添加功能的方法。 优点是不会通过更新WordPress删除它们,因此您可以确保始终检索到您的功能。

A mu-plugin is still a plugin. When you want to add feature to WordPress, you can often chose to either use a classic plugin or a must-use plugin without the risk of not retrieving a function you need, unless that function is an activation hook. The only difference between these two types of plugins is that the must-use plugins will always be executed and they can’t be deactivated. So if you ask yourself whether you must choose classic plugins or must-use ones, seeing mu-plugins as an extension of WordPress can be a good idea.

mu插件仍然是插件。 当您想向WordPress添加功能时,通常可以选择使用经典插件或必须使用的插件,而不必承担检索不到所需功能的风险,除非该功能是一个激活钩子。 这两种类型的插件之间的唯一区别是必须使用的插件将始终被执行,并且无法将其停用。 因此,如果问自己是必须选择经典插件还是必须使用的插件,那么将mu-plugins作为WordPress的扩展是一个好主意。

Finally, there is another disadvantage of must-use plugins which we did not cover above. WordPress won’t tell you if an update exists for mu-plugins, so if you use one that you did not write yourself, you will have to verify by yourself if an update exists.

最后,必须使用的插件还有另一个缺点,我们在上面没有提到。 WordPress不会告诉您mu插件是否存在更新,因此,如果您使用的不是自己编写的插件,则必须自己确认是否存在更新。

The examples of this tutorial can be retrieved here, where you will find an archive containing four mu-plugins: the ‘Hello World!’ example and another example which loads two other ‘Hello World!’ type plugins to show that we can store mu-plugins in subdirectories. To test these examples, create the mu-plugins subdirectory into the wp-content folder and place the files and folders of the archive in this subdirectory.

可以在此处找到本教程的示例,在这里您将找到一个包含四个mu插件的存档:“ Hello World!”。 这个例子和另一个载入另外两个“ Hello World!”的例子 键入插件以表明我们可以将mu插件存储在子目录中。 要测试这些示例,请在wp-content文件夹中创建mu-plugins子目录,然后将归档文件和文件夹放在此子目录中。

翻译自: https://www.sitepoint.com/wordpress-mu-plugins/

wordpress插件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值