WordPress Core includes a set of functions and hooks that control certain aspects of the network,
or individual sites on the network. In this lesson you'll learn about multi-site specific
function naming conventions to be aware of, and then dive into some useful multi-site specific
functions and hooks you can use. Finally, you'll learn about developing for individual sites on
the network, as well as where to go for more information about multi-site functions and hooks.
There are a couple of different naming conventions used in the WordPress codebase when it comes to
multi-site. WordPress multi-site was originally known as WordPress MU or WordPress Multi-User,
and many multi-site related functions and hooks still use the WPMU prefix.
Additionally, some functions are named based on the old terminology,
which describe multiple blogs on a site. This has since been updated to describe multiple sites on
a network instead, but some old terminology still lives on in some function names.
When you're developing a product to support a multi-site network,
there are some useful internal functions worth knowing about.
The first is the is multi-site function. This function will return true if multi-site is
enabled, and is probably the most widely used function related to multi-site.
If you do a search through the WordPress codebase for all the users of the is multi-site function,
you'll see that it's used in a number of places to either perform specific tasks in the context of
a multi-site network or to restrict functionality only to multi-site networks.
There are also some common functions that are useful when developing administration
interfaces for the multi-site network. isSuperAdmin can be used to check if the
currently logged in user is a network administrator on the network.
isNetworkAdmin is the multi-site equivalent of the isAdmin function
and determines whether the current request is for the network administrative interface.
networkAdminURL is the multi-site equivalent of the adminURL function and allows you to
create URLs relative to the admin area of the network. This is useful for redirecting to
different areas of the network admin dashboard. When working with site content, there are some
functions that are widely used. isMainSite determines whether the current site is the
main site of the network or not. Next, there is the getSites function, which will return a list
of sites matching requested arguments. Then there is the switchToBlog function, which allows you
to switch to a different site on the network. restoreCurrentBlog, which restores the current
site after you switch to a different site. and getCurrentBlogID, which returns the ID of the
current site. Using these functions, you can perform actions across the network.
For example, let's say you wanted to create a function that updated an option on a specific
site on the network. Here, you're using the switchToBlog function to switch to that site by its ID,
then using the updateOption function to update the option by name, passing in the value,
and then finally restoring to the current site on the network. However, if you wanted to extend
that to update the same option across all sites, you could use the getSites function
and loop through all the sites on the network. So we could start by removing the siteID parameter,
then get a list of sites using the getSites function,
and then loop through all the sites in a forEach loop.
We can move this code into the loop
and update the site by the blogID property on the site object.
You could also use the updateBlogOption function to update an option on a specific site without
having to switch to that site. So instead of doing the switching, we can use updateBlogOption
and we pass it in the siteID
and the option name and value.
When developing multi-site plugins, there is the isNetworkOnly plugin function.
This is a plugin-specific function that checks for the network true value in the plugin header
to see if this should be activated only as a network-wide plugin. This is useful if you want
to restrict a plugin to only be activated on the network and not on individual sites.
There are also a couple of useful hooks that you can use when developing for multi-site.
The first is the networkAdminMenu hook, which allows you to add a menu item to the network
admin dashboard. The second is the networkAdminNotices hook, which allows you to add notices to the
network admin dashboard. This is the multi-site equivalent of the admin notices hook that is used
for single-site notices. The signupBlogForm filter is a filter that allows you to modify the signup
form for new sites. You can use this to add additional fields to the signup form.
Finally, WP initialize site is an action that is fired when a new site is created on the network.
This is useful if you want to perform actions when a new site is created, for example, if you wanted
to assign a custom top-level domain to a new single site. When you are rendering any content in the
scope of a single site on the network, WordPress Core is clever enough to know that you're working
inside the scope of that site. This means that any functions that you use to retrieve information
such as get blog info, get option, get posts, or get post meta, and any function that you might
use to add or update information like update option, WP insert post, or update post meta,
will get, add, or update the correct tables for the site that you are currently working with.
Additionally, if you use functions like registerPostType or registerTaxonomy,
these will be registered for the current site only.
For a full list of all WordPress multi-site related functions and hooks,
check out the multi-site package category in the WordPress Developer Reference.