WP-CLI

Updated: 12 October 2024

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

Scaffold a plugin

wp scaffold plugin citypay-capture \
--plugin_name="CityPay Capture" \
--plugin_description="TODO: add description." \
--plugin_author="Chris Taylor Developer" \
--plugin_author_uri=https://christaylordeveloper.co.uk \
--plugin_uri=https://christaylordeveloper.co.uk

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 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 debugging

Updated: 28 March 2024

To enable debugging generally, add these lines to file wp-config.php just before /* That's all, stop editing! Happy publishing. */

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

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

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

Write any message to /wp-content/debug.log

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

Write a PHP variable to the error log with print_r

error_log(print_r($var, true));

Write a PHP variable to the error log with var_dump

function error_log_var_dump( $var ){
    ob_start();
    var_dump( $var );
    $cont = ob_get_contents();
    ob_end_clean();
    error_log( $cont );
}