Skip to content

Instantly share code, notes, and snippets.

@vanpariyar
Last active May 23, 2024 11:01
Show Gist options
  • Save vanpariyar/8e4a124cf57810a5727cdf7f4cb43676 to your computer and use it in GitHub Desktop.
Save vanpariyar/8e4a124cf57810a5727cdf7f4cb43676 to your computer and use it in GitHub Desktop.
Some of the useful WordPress VIP CLI & WP CLI commands curated list

Individual site If you have a site with slug blog on a WordPress multisite instance using subdomains, you can use the full URL to run a command against the site:

wp --url=blog.example.com

Think of the --url= parameter as any URL you can put in your browser.

Multiple sites Need to run a given WP-CLI command against multiple sites on a WordPress multisite install? You can do so using a combination of wp site list and xargs.

Here’s an example where wp option update is run against all sites:

wp site list --field=url | xargs -n1 -I % wp --url=% option update my_option my_value

Deconstructing this example:

wp site list --field=url produces a list of URLs for all of the sites on the network. You can configure the list of URLs by filtering based on the supported arguments. The | operator passes the results from wp site list to xargs, a utility which takes lines of input and passes them to some command. xargs -n1 -I % wp --url=% option update my_option my_value runs wp option update my_option my_value against a given site, identified with --url=. The % is a placeholder that’s replaced with a URL from the site list.

if you are using VIP add this line before command vip @application-name.environment -- wp COMMAND Create Users Multisite

wp user create bob [email protected] --role=administrator --user_pass=Pasword

Update user List in one website

wp user update my_username --url=other-multisite.com --role=administator

Add user to all multisite

wp user create bob [email protected] --role=administrator --user_pass=Pasword
wp site list --field=url | xargs -I % wp user set-role <user-login> <role> --url=%

So, if a user exists in multisite, then how do you go about adding them to a particular blog then?

wp --url=subsite.example.com user set-role <user-login> <role>

some useful links Simply download the following http-to-https.php file and run:

wp site list --field=url | xargs -I % wp eval-file http-to-https.php --url=%

http-to-https.php

<?php
/**
 * Update a specific site from 'http://' to 'https://'.
 *
 * Only touches the 'home' and 'siteurl' options.
 * Depending on plugins, etc., you may need to update other options too.
 *
 * Run on WordPress multisite with:
 *
 * wp site list --field=url | xargs -I % wp eval-file http-to-https.php --url=%
 */

foreach ( array( 'home', 'siteurl' ) as $option ) {
	$existing = get_option( $option );
	$new = str_replace( 'http://', 'https://', $existing );
	if ( update_option( $option, $new ) ) {
		WP_CLI::log( "Updated '{$option}' to '{$new}'" );
	} else {
		WP_CLI::log( "Didn't update '{$option}' to '{$new}'" );
	}
}
WP_CLI::success( 'Options updated for ' . home_url() );

Remove the website taxonomy

<?php
function remove_taxonomy(){
	global $wpdb;

	$taxonomy_name = 'the_taxonomy_to_delete';
	 Deletes all the terms associated to the taxonomy.
	# The inner query collects from the 'term_taxonomy' 
	# table all the ids of the terms to delete.
	$wpdb->query( 
	  "DELETE FROM {$wpdb->terms} AS t 
	   WHERE t.term_id IN (
	    SELECT tt.term_id FROM {$wpdb->term_taxonomy} AS tt
	    WHERE tt.taxonomy = '{$taxonomy_name}'
	   )"
	);

	# Deletes the information used by WordPress 
	# to link each post to their terms.
	#
	# Here too, a nested query is needed, 
	# and the 'term_taxonomy' table is yet again 
	# the source of the data passed to the outer query.
	# But in this case to be collected by 
	# the inner query are the ids of 
	# the Relationships to delete.
	$wpdb->query( 
	  "DELETE FROM {$wpdb->term_relationships} AS tr 
	   WHERE tr.term_taxonomy_id IN (
	    SELECT tt.term_taxonomy_id FROM {$wpdb->term_taxonomy} AS tt
	    WHERE tt.taxonomy = '{$taxonomy_name}'
	   )"
	);

	# Deletes the base information about the taxonomy. 
	$wpdb->query( 
	  "DELETE FROM {$wpdb->term_taxonomy} 
	   WHERE taxonomy = '{$taxonomy_name}'"
	);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment