WP-CLI

Updated: 03 June 2023

https://wp-cli.org/

Example commands

wp core download --version=6.1.1 --force --skip-themes --skip-plugins
wp core download --version=6.2.2 --skip-content
wp cache flush --all --allow-root
wp core download --version=6.2.2 --skip-content
wp core download --force --version=6.2.2 --skip-content
wp option get home

Searching for WordPress users by id.

Updated: 01 November 2022

From the Users screen

Searching for a WordPress user by ID is fairly straightforward from the Users Screen. Simply enter a user ID in the ‘Search Users’ box and press the button. Although the documentation (https://wordpress.org/support/article/users-screen/#search) states:
Any User that contains the search string in the Username, Name, E-mail, or Website fields will be displayed, by Role.
searching by ID also works.

Looking at the source code for class WP_Users_List_Table (https://core.trac.wordpress.org/browser/tags/6.0.2/src/wp-admin/includes/class-wp-users-list-table.php#L141) the user search is carried out using an instance of class WP_User_Query. If the search columns are not specified default search columns are set depending on the search term. If the search term is numeric the search columns default to user_login and ID (see https://core.trac.wordpress.org/browser/tags/6.0.2/src/wp-includes/class-wp-user-query.php#L717). Incidentally, this means that purely numeric search terms will not match email addresses.

From the command line

The syntax for searching for a WordPress user by ID using WP CLI is a bit obscure. The relevant section of the documentation for wp user list is:

[--<field>=<value>]
    Control output by one or more arguments of WP_User_Query().

The documentation for WP_User_Query includes the search argument, which is the necessary field. (See https://developer.wordpress.org/reference/classes/wp_user_query/#search-parameters).

Example: wp user list --search=317

As a side note, it is also possible to search by role using WP CLI.

Example: wp user list --role=subscriber

WordPress – unable to create directory

Updated: 16 January 2022

Resolution for ‘Unable to create directory’ error in WordPress.

This error is caused by an inappropriate set of folder and file permissions. The following statements assume you have shell access to the document root of your webserver.

Set owner and group of each folder and file to the webserver user
sudo chown -R www-data:www-data /var/www/html

Set permissions for all folders to 755
sudo find /var/www/html -type d -exec chmod 755 {} \;

Set permissions for all files to 644
sudo find /var/www/html -type f -exec chmod 644 {} \;

For problems uploading images, also check the uploads folder is set appropriately in WordPress Dashboard > Settings > Media. If already set to the default wp-content/uploads, this field may not be visible.

WordPress maintenance mode

Updated: 10 September 2022

Use in theme functions.php or a plugin to put WordPress into maintenance mode.

function wp_maintenance_mode() {
    if ( ! current_user_can('edit_themes' ) || ! is_user_logged_in() ) {
        wp_die('Under Maintenance.');
    }
}
add_action( 'get_header', 'wp_maintenance_mode' );

WordPress error log

Updated: 28 May 2023

Write a variable to the error log with print_r

error_log(print_r($var, true));

Write a variable to the error log via var_dump

function error_log_var_dump( $var ){
    ob_start();
    var_dump( $var );
    $cont = ob_get_contents();
    ob_end_clean();
    error_log( $cont );
}
// Enable WP_DEBUG mode
define( 'WP_DEBUG', true );

// Enable Debug logging to the /wp-content/debug.log file
define( 'WP_DEBUG_LOG', true );

// Display errors and warnings
define( 'WP_DEBUG_DISPLAY', true );
@ini_set( 'display_errors', 1 );

WordPress debugging

Updated: 24 June 2023

To turn on debugging add the following to wp-config.php

// Turn debugging on
define('WP_DEBUG', true);

// Tell WordPress to log everything to /wp-content/debug.log
define('WP_DEBUG_LOG', true);

// Display error messages
define('WP_DEBUG_DISPLAY', true);

Write to /wp-content/debug.log

error_log('logging message here...');