# WordPress
- Guide: Setup Local Environment with Lando
- Composer and WordPress
- Helpful commands for WP CLI
- Optimisation in WP
- WordPress Multisite
- Helpful resources
# Update WP and Plugins using PHP Composer
If you don’t have Composer installed on your local system, or you need to run it on a container that’s on a different version of PHP, prefix the command with lando to use the bundled version on composer.
Update plugin and WP versions specified in composer.json:
composer update
Install a specific package without updating all packages:
composer require package/package:version
Remove a specific package:
composer remove package/package:version
Update specific packages without updating everything:
composer update package/package:version
If you see an issue related to vendor locally, try running composer install
# Helpful commands for WP CLI
Use these for general maintenance. The wp plugin and wp core commands are only for use on non PSH sites as you must use composer to manage plugins and core versions on PSH. If running these commands locally, prefix with lando.
Create a new user:
wp user create <user> <user-email> --role=<administrator>
Update the password of the user:
wp user update <user> --user_pass='<password>'
Update WordPress Version (perform DB backup first):
wp core update
List all plugins installed on website:
wp plugin list
Update plugins:
wp plugin update <plugin-name> (can be used with --dry-run)
List update available for plugin:
wp plugin update <plugin-name> --version=2.0.0
Update the permissions for a directory
chmod -R 755 .
Perform a Search and Replace of the DB
wp search-replace <old-url.dusted> <new-url.lndo.site>
# Optimisation in WP
PSH gzips static files (CSS/JS). It’s best to not use Autoptimize. Rely on WP settings to move JS to footer to improve speeds.
# Load scripts in the footer
Set all scripts to load in footer by setting the 5th argument in wp_enqueue_script to true
wp_enqueue_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js', array(), '3.6.0', true);
Remove unecessary core styles
wp_dequeue_style( 'wp-block-library' );
# WP Super Cache
This is probably the most important step we can take in terms of optimising the speed of sites. This plugin will generate a compressed static version of every page on a site, drastically improving the time to load as there’s no DB or PHP work for the server. PSH wont allow the advanced-cache.php or wp-cache-config.php files that the plugin needs to be written to wp-content so some configuration needs to be done to set this up.
Ensure WP Super Cache is installed
lando composer require wpackagist-plugin/wp-super-cache
Build hook to copy advanced-cache.php to wp-content
Add a cp command to the platform build hook in platform.app.yaml to copy the advanced-cache.php file from the plugin folder to wp-content
cp -r wordpress/wp-content/plugins/wp-super-cache/advanced-cache.php wordpress/wp-content/advanced-cache.php
Set up a new location and mount in platform.app.yaml
Location
"/wp-content/cache-config":
root: "wordpress/wp-content/cache-config"
scripts: true
allow: true
Mount
"wordpress/wp-content/cache-config":
source: local
source_path: "cache-config"
Add cache definitions to wp-config.php
Edit wp-config.php in the root
define('WP_CACHE', true);
define('WPCACHEHOME', WP_CONTENT_DIR . '/plugins/wp-super-cache/');
define('WPCACHECONFIGPATH', WP_CONTENT_DIR . '/cache-config');
Run lando composer install after editing this file to copy it back into the wordpress folder.
Generating the mount folders
If you want to check the steps above locally before deployment, manually create the mount folder in wordpress/wp-content.
Alternatively, push the changes to PSH and then run lando pull to generate the missing mount folders.
Set Super Cache to compress pages
Tick the compress pages option in the plugin’s setting page in the CMS.
# WordPress Multisite
# Cloning multisite when hosted on Platform [last updated: 17th of November, 2022]
Before you get started, have all of the multisite setup rules commented out in wp-config. Note that these rules may not apply to every scenario, but have helped set up local versions of the wordpress multisites. They are also specific to pathbased configuration (i.e. seperate URLs for each site), rather than domain based configuration (seperate domains).
# Performing a backup of the database
- Check if your
wp-config.phpcontains all of the rules setup for enabling multisite:
/* Multisite */
define('WP_ALLOW_MULTISITE', true );
define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', false);
define('DOMAIN_CURRENT_SITE', $site_host);
define('PATH_CURRENT_SITE', '/');
define('SITE_ID_CURRENT_SITE', 1);
define('BLOG_ID_CURRENT_SITE', 1);
- Comment out the above for the time being (this should be done in the same version of your config as the active wordpress installation, i.e.
wordpress/wp-config.php). - Perform a manual backup of the remote database:
platform db:dump. - Open the backup of the database in your editor and perform a search on
wp_blogs. Look for the block of code similar to the below and update this to your local URL version (i.e.excelsior-finance.lndo.site) Note that this step may be experimental and unneccerssary as you will perform a search and replace again, once you have imported the database:
INSERT INTO `wp_blogs` VALUES (1,1,'www.excelisor-finance.com','/','2022-11-04 17:43:21','0000-00-00 00:00:00',1,0,0,0,0,0);
# Importing the database
- Import the database locally (note that whilst there are different databases for each environment on Platform, lando only use one database, across all of it's branches):
lando database main < platform-generated-backup.sql. - Clear your local cache (you may need to repeat this step often during the process):
lando wp cache flush. - Perform a search and replace on the main site. Note that each multisite has a seperate database, so it may be beneficial to perform a search and replace on every URL (remember to clear your cache after this step):
# main site
lando wp search-replace www.excelsior-finance-fzxvcey-7ww7dp5dayqoq.uk-1.platformsh.site www.excelsior-finance.lndo.site --url=www.excelsior-finance.lndo.site
# investors site
lando wp search-replace www.excelsior-finance-fzxvcey-7ww7dp5dayqoq.uk-1.platformsh.site www.excelsior-finance.lndo.site --url=www.excelsior-finance.lndo.site/investors
# company site
lando wp search-replace www.excelsior-finance-fzxvcey-7ww7dp5dayqoq.uk-1.platformsh.site www.excelsior-finance.lndo.site --url=www.excelsior-finance.lndo.site/company
# The local version of the multisite keeps redirecting to staging version.
You may need to have a play with updating $site_host in your wp-config.php to your local URL, and repeating some of the steps above (also remember to clear your cache):
define('DOMAIN_CURRENT_SITE', 'www.excelsior-finance.lndo.site');
# Helpful links and resources
- Generate salts (opens new window).
- Learn more about updating plugins here (opens new window).
← Sustainability Drupal →