If you’re asking how to manage your WordPress multisite network using WP-CLI, the short answer is: almost entirely. While you’ll still pop into the admin occasionally for things like theme customization or plugin settings that don’t expose their configurations to the command line, pretty much every core administrative task, from creating sites to managing users and updating everything, can be handled much faster and more efficiently via WP-CLI. This is a game-changer for speed, automation, and consistent management across your network.
Getting Started with WP-CLI for Multisite
Before we dive into the specifics, make sure you have WP-CLI installed and configured for your WordPress installation. If you’re running a multisite, WP-CLI automatically recognizes it as such and provides network-specific commands. It’s a good idea to perform a quick wp --info to ensure everything is working as expected.
One crucial concept to grasp is context. When you run a command, WP-CLI needs to know where to run it. For a multisite, this means running commands either globally on the network (e.g., wp site list) or on a specific subsite (e.g., wp post list --url=example.com/subsite). Many commands that operate on a single site can accept the --url or --url= parameter to target them.
For those looking to enhance their understanding of managing WordPress multisite networks through the command line, a related article that provides valuable insights is available at The Sheryar. This resource delves into various WP-CLI commands and best practices, making it an excellent companion for anyone aiming to streamline their multisite management process.
Managing Sites and Network Settings
The bread and butter of multisite management is, well, managing the sites themselves. WP-CLI excels here, allowing you to create, modify, and delete sites with simple commands, alongside handling network-wide settings.
Creating and Deleting Sites
Need a new site? Forget the clicks and endless forms. A single command is all it takes.
- Adding a new site:
“`bash
wp site create –slug=mynewsite –title=”My New Site” –email=admin@example.com –url=mynewsite.example.com
“`
This command is pretty straightforward. You define the slug, title, an admin email, and the full URL. For subdirectory installs, the --url might just be example.com/mynewsite.
- Listing existing sites:
“`bash
wp site list
“`
This gives you an overview of all sites on your network, including their ID, URL, title, and last updated date. Super handy for quickly grabbing a site ID or just seeing what’s there.
- Deleting a site:
“`bash
wp site delete
“`
Replace with the actual ID from wp site list. Be careful, this is permanent! There’s no trash bin for sites deleted via WP-CLI.
- Archiving a site:
If you don’t want to permanently delete it, but just take it offline:
“`bash
wp site archive
“`
- Unarchiving a site:
Bring it back from the archives:
“`bash
wp site unarchive
“`
Updating Site Information
Once a site exists, you might need to change its details – the title, status, or even its URL.
- Updating a site’s details:
“`bash
wp site update
“`
You can update fields like title, public, archived, mature, spam, deleted, lang_id.
- Changing a site’s URL:
This is a big one and needs to be done carefully, especially if domain mapping is involved.
“`bash
wp site update
“`
Remember to adjust your server configuration (web server, DNS) to reflect the new domain if you’re truly moving it. WP-CLI only changes the database entry.
- Getting a specific site’s details:
“`bash
wp site get
“`
This will output all the metadata associated with a specific site, which is useful for debugging or verifying changes.
Managing Network Options
Beyond individual sites, WP-CLI lets you tinker with global network settings.
- Listing network options:
“`bash
wp option list –network
“`
This shows all options stored in the wp_sitemeta table, which are global to the network.
- Getting a specific network option value:
“`bash
wp option get network_admin_email –network
“`
Replace network_admin_email with the option key you’re interested in.
- Updating a network option:
“`bash
wp option update network_admin_email newadmin@example.com –network
“`
This is how you would change the primary administrator email for the entire network.
User and Role Management Across the Network
Users interact with your network at various levels: network admin, subsite admin, editor, etc. WP-CLI provides granular control over user management, allowing you to add, remove, and modify user roles across different sites.
Adding and Removing Users
- Creating a user network-wide:
“`bash
wp user create username user@example.com –first_name=John –last_name=Doe –role=subscriber –send-email
“`
This creates a user in the global wp_users table. They don’t automatically have access to any specific subsite until added. Using --send-email will send the new user their login details.
- Adding an existing user to a specific site:
“`bash
wp user add-super-admin username
“`
This adds a user as a super admin to the entire network, granting them full control over all sites. Use with caution.
- Removing a user from a specific site:
“`bash
wp user remove-super-admin username
“`
This removes a user’s super admin privileges.
- Adding a user to a specific subsite with a role:
“`bash
wp user add-user
“`
No, wait, this is not directly how it works in WP-CLI. Instead, you’d use wp user set-role:
“`bash
wp user set-role
“`
This command assigns the editor role to the user with on the site specified by --url. If the user doesn’t exist on that site yet, this command will add them and set their role.
- Removing a user from a specific subsite:
“`bash
wp user remove-user
“`
This removes the user’s association with that particular subsite. The user still exists globally in the network.
- Listing all users on the network:
“`bash
wp user list
“`
This shows all users in the wp_users table, regardless of their role on specific sites.
- Listing users for a specific site:
“`bash
wp user list –url=mynewsite.example.com
“`
This provides a list of users specifically associated with that subsite and their roles on it.
Managing User Roles
WordPress multisite has global roles (super admin) and per-site roles.
- Adding a Super Administrator:
“`bash
wp super-admin add
“`
This grants full network-wide administration privileges to a user.
- Removing a Super Administrator:
“`bash
wp super-admin remove
“`
- Listing Super Administrators:
“`bash
wp super-admin list
“`
A quick way to see who has the keys to the kingdom.
Plugin and Theme Management Across the Network
One of the greatest benefits of multisite with WP-CLI is the ability to manage plugins and themes network-wide or on individual sites efficiently. No more clicking through each site’s admin panel!
Network-Activating and Deactivating Plugins
Plugins can be installed globally and then activated on individual sites, or network-activated so they are active on all new and existing sites.
- Installing a plugin:
“`bash
wp plugin install akismet –activate-network
“`
This installs Akismet and immediately activates it network-wide. If you omit --activate-network, it’s just installed and available for activation.
- Listing all plugins (network-wide):
“`bash
wp plugin list –network
“`
This gives you a full list of all plugins installed on your network and their network activation status.
- Activating a plugin network-wide:
“`bash
wp plugin activate akismet –network
“`
This makes a plugin active for all sites on the network.
- Deactivating a plugin network-wide:
“`bash
wp plugin deactivate akismet –network
“`
- Updating all plugins on the network:
“`bash
wp plugin update –all –network
“`
This is extremely powerful. One command to update every single plugin across your entire network.
- Updating individual plugins:
“`bash
wp plugin update akismet –network
“`
If you only want to update specific plugins.
- Uninstalling a plugin (network-wide):
“`bash
wp plugin uninstall akismet –network
“`
This will deactivate and remove the plugin files for the entire network.
Managing Plugins on Specific Sites
Sometimes a plugin is only needed on one or a few sites, not globally.
- Listing plugins on a specific site:
“`bash
wp plugin list –url=mynewsite.example.com
“`
This lists only the plugins active on that specific subsite, including those activated network-wide.
- Activating a plugin on a specific site:
“`bash
wp plugin activate my-custom-plugin –url=mynewsite.example.com
“`
The plugin must already be installed on the network.
- Deactivating a plugin on a specific site:
“`bash
wp plugin deactivate my-custom-plugin –url=mynewsite.example.com
“`
Network-Enabling and Disabling Themes
Themes are managed similarly to plugins, but themes are “network enabled” to be available for individual sites to choose from. You can’t network-activate a theme in the same way you activate a plugin everywhere; instead, you make it available for individual sites.
- Installing a theme:
“`bash
wp theme install twentytwentyfour –activate-network
“`
The --activate-network argument with themes actually enables it for all sites to use, making it available in the themes browser on each subsite.
- Listing all themes (network-wide):
“`bash
wp theme list –network
“`
This shows all installed themes and their network-enabled status.
- Enabling a theme network-wide:
“`bash
wp theme enable twentyseventeen –network
“`
This makes twentyseventeen available for all subsites to activate.
- Disabling a theme network-wide:
“`bash
wp theme disable twentyseventeen –network
“`
This removes the theme from the list of available themes for subsites (unless a subsite is currently using it, which means it will remain active on that subsite until changed).
- Updating all themes on the network:
“`bash
wp theme update –all –network
“`
Updates every installed theme.
- Uninstalling a theme (network-wide):
“`bash
wp theme uninstall twentytwentyfour –network
“`
Removes the theme files, assuming no site is actively using it.
Managing Themes on Specific Sites
While you can’t network-activate a theme in the same sense as a plugin, you can set a theme for a specific site.
- Activating a theme on a specific site:
“`bash
wp theme activate twentytwentythree –url=mynewsite.example.com
“`
This activates twentytwentythree as the active theme for mynewsite.example.com. The theme must be network-enabled or installed directly on the subsite (though the latter is less common in multisite).
- Listing themes for a specific site:
“`bash
wp theme list –url=mynewsite.example.com
“`
Shows themes available and active for that particular site.
Managing a WordPress multisite network can be a complex task, but utilizing WP-CLI can significantly streamline the process. For those looking to enhance their server management skills, you might find it beneficial to explore how to send emails using CyberPanel, which can complement your multisite setup by ensuring reliable email communication. You can read more about this topic in the article here. By integrating these tools, you can create a more efficient and effective WordPress environment.
Network Maintenance and Optimization
Beyond basic management, WP-CLI provides tools for keeping your multisite running smoothly, from database optimization to routine updates.
Updating WordPress Core
Keeping your WordPress core up to date is paramount for security and performance.
- Checking for core updates:
“`bash
wp core check-update –network
“`
This tells you if a new version of WordPress is available for your network.
- Updating WordPress core:
“`bash
wp core update –network
“`
This will update the entire WordPress core installation for your multisite network.
- Updating core database:
After a core update, sometimes the database needs to be updated too:
“`bash
wp core update-db –network
“`
It’s good practice to run this after any core update.
Database Optimization
Multisite databases can become quite large and fragmented over time.
- Optimizing database tables:
“`bash
wp db optimize –network
“`
This command optimizes all tables in your network’s database, which can help improve performance.
- Repairing database tables:
“`bash
wp db repair –network
“`
If you suspect database corruption, this command can help repair tables.
Transients and Caching
Transients are temporary cached data that can sometimes pile up.
- Deleting all transients across the network:
“`bash
wp transient delete –all –network
“`
This can clean out old, stale, or buggy transient data, potentially resolving issues or improving performance.
- Flushing cache (if using object cache plugins):
Many caching plugins integrate with WP-CLI. For example, with WP Super Cache:
“`bash
wp cache flush –network
“`
This would clear the cache for the entire network. The exact command depends on your caching solution.
Running Global Commands on All Sites
One of the most powerful aspects of WP-CLI for multisite is the ability to execute the same command on every single subsite.
- Running a command on all sites:
“`bash
wp site list –field=url | xargs -I % wp post delete –all –url=%
“`
This example gets a list of all site URLs and then, for each URL, deletes all posts. Be extremely careful with commands like this on a production network! This pattern can be used for any command.
- Example: Updating permalinks on all sites:
Sometimes, after migration or changes, permalinks might need to be flushed on all sites. There isn’t a direct “update permalinks” command, but you can achieve it by updating a setting twice.
“`bash
wp site list –field=url | xargs -I % sh -c ‘wp option update permalink_structure “/%postname%/” –url=% && wp option update permalink_structure “” –url=% && wp option update permalink_structure “/%postname%/” –url=%’
“`
This is a rather clumsy example, simply updating an option, but it shows the potential. More realistically, you might use a wp rewrite command if available for specific permalink flushing logic. A more standard way to flush rewrites on a site is:
“`bash
wp rewrite flush –url=mynewsite.example.com
“`
And to do it on all sites would involve the xargs pattern.
Advanced Automation and Scripting with WP-CLI
The real magic of WP-CLI in a multisite context comes when you start combining commands and using them in scripts. This allows for powerful automation of routine tasks, disaster recovery, and large-scale changes.
Creating Bash Scripts for Common Tasks
Consider a scenario where you create new sites regularly, each needing a set of default plugins, themes, and perhaps some initial content.
- Example: New Site Provisioning Script:
“`bash
#!/bin/bash
NEW_SITE_SLUG=$1
NEW_SITE_TITLE=$2
NEW_SITE_EMAIL=$3
NEW_SITE_URL=$4
if [ -z “$NEW_SITE_SLUG” ] || [ -z “$NEW_SITE_TITLE” ] || [ -z “$NEW_SITE_EMAIL” ] || [ -z “$NEW_SITE_URL” ]; then
echo “Usage: $0
exit 1
fi
echo “Creating site: $NEW_SITE_TITLE ($NEW_SITE_URL)”
wp site create –slug=”$NEW_SITE_SLUG” –title=”$NEW_SITE_TITLE” –email=”$NEW_SITE_EMAIL” –url=”$NEW_SITE_URL”
Get the ID of the newly created site
SITE_ID=$(wp site list –field=blog_id –url=”$NEW_SITE_URL”)
echo “Site created with ID: $SITE_ID”
if [ -n “$SITE_ID” ]; then
echo “Activating default theme (twentytwentyfour)…”
wp theme activate twentytwentyfour –url=”$NEW_SITE_URL”
echo “Activating common plugins…”
wp plugin activate classic-editor –url=”$NEW_SITE_URL”
wp plugin activate yoast-seo –url=”$NEW_SITE_URL”
echo “Adding an initial post…”
wp post create –post_type=post –post_status=publish –post_title=”Welcome to ${NEW_SITE_TITLE}” –post_content=”This is your first post on ${NEW_SITE_TITLE}.” –url=”$NEW_SITE_URL”
echo “New site ‘$NEW_SITE_TITLE’ provisioned successfully!”
else
echo “Error: Could not retrieve site ID for $NEW_SITE_URL. Site creation might have failed.”
fi
“`
You would save this as, say, provision-new-site.sh, make it executable (chmod +x provision-new-site.sh), and then run it:
“`bash
./provision-new-site.sh mynewblog “My Awesome Blog” blogadmin@example.com mynewblog.example.com
“`
Integrating with Cron Jobs
For routine maintenance, like database optimization or clearing transients, scheduling these WP-CLI commands via cron jobs is immensely useful.
- Example Cron Entry:
“`cron
0 3 * cd /var/www/html/wordpress && wp db optimize –network >> /var/log/wp-cli-cron.log 2>&1
0 4 * cd /var/www/html/wordpress && wp transient delete –all –network >> /var/log/wp-cli-cron.log 2>&1
“`
This schedules database optimization at 3 AM and transient cleanup at 4 AM daily. Adjust the path (/var/www/html/wordpress) to your WordPress installation directory.
WP-CLI transforms multisite management from a click-heavy, time-consuming chore into a streamlined, scriptable, and incredibly efficient process. Embrace it, and you’ll wonder how you ever managed your network without it.