Skip to content

Instantly share code, notes, and snippets.

View michael-sumner's full-sized avatar
:electron:
Making the world better with incredible digital experiences

Michael Sumner michael-sumner

:electron:
Making the world better with incredible digital experiences
View GitHub Profile
@michael-sumner
michael-sumner / commands.sh
Created October 22, 2024 12:37
WPVIP - loop through all sites in network - set user to administrator role
# 1. Save the commands.sh file somewhere (no need to commit to repository).
# 2. Run `chmod +x ./commands.sh` to allow permission to run the file.
# 3. Run `./commands.sh` to run the file.
env="@99999.develop"
sites=$(vip $env -- wp site list --field=url)
for site in $sites; do
vip $env -- wp user set-role michaelsumner administrator --url=$site
done
@michael-sumner
michael-sumner / index.md
Created October 10, 2024 16:23
How to import databases into LocalWP quickly
  1. Create a new LocalWP Site.
  2. Unzip the db_name.sql.gz into the /app/public directory
  3. Open the Site Shell
  4. Visit the MySQL subshell using the command:
wp db query
@michael-sumner
michael-sumner / core.php
Created September 16, 2024 12:36
Co-Authors Plus - add custom "guest-author" fields
<?php
add_filter( 'coauthors_guest_author_fields', 'add_guest_author_fields', 10, 2 );
/**
* Adds the custom guest author fields to the coauthors guest author fields array.
*
* @param array $fields_to_return The fields to return.
* @param array $groups The groups to return.
* @return array The fields to return.
@michael-sumner
michael-sumner / index.sh
Last active June 11, 2024 09:47
Loop through all posts and re-save them, which helps trigger the save_post hook. Example post type: "resource" is used. `--no-store` helps prevent an empty required value.
wp post update $(wp post list --post_type=resource --field=ID) --nostore
@michael-sumner
michael-sumner / shell.sh
Last active May 23, 2024 11:52
Set the existing user as administrator role across all sites in the WordPress multi-site network
# wp site list --field=url | xargs -I % sh -c 'wp user set-role <user-login> <role> --url=%'
#
# For example:
wp site list --field=url | xargs -I % sh -c 'wp user set-role testuser administrator --url=%'
@michael-sumner
michael-sumner / pantheon-test-command.sh
Last active March 4, 2024 09:02
Pantheon terminus - How to Loop in Subshell Commands
# Example: loop through all post ids and update the post meta for each post_id to assign the post_date
for post_id in $(terminus remote:wp examplesite.environment -- post list --format=ids); do terminus remote:wp examplesite.environment -- post meta update "$post_id" examplemeta_start_date "$(terminus remote:wp examplesite.environment -- post get $post_id --field=post_date)"; done
@michael-sumner
michael-sumner / bash.sh
Last active October 23, 2023 17:56
WP-CLI - List all user roles with specific capability
capability="upload_files"
wp role list --fields=role --format=csv | tail -n +2 | while IFS= read -r role; do
if wp cap list "$role" | grep -q $capability; then
echo "$role"
fi
done
@michael-sumner
michael-sumner / index.sh
Created September 14, 2023 11:17
WP-CLI command to loop through all sites in the multisite network and use wp db query to obtain a list of posts, and save them in log.txt file
# replace "Hello, World!" with appropriate string to search for
# replace `post_title` with appropriate column to search for
wp site list --fields=url,blog_id | while IFS= read -r line; do
url=$(echo "$line" | awk -F'\t' '{print $1}')
blog_id=$(echo "$line" | awk -F'\t' '{print $2}')
wp post list --field=url --ignore_sticky_posts=1 --orderby=date --order=DESC --post__in=$(wp db query "SELECT ID FROM wp_${blog_id}_posts WHERE post_title LIKE '%Hello, World!%' AND post_status='publish' AND post_type='post'" --skip-column-names --url=${url} | paste -s -d ',' -) --url=${url}
done >> log.txt
@michael-sumner
michael-sumner / index.sh
Created September 8, 2023 13:54
WP-CLI to check which sites in a multisite network have a specific plugin activated
wp site list --field=url | xargs -I % sh -c 'wp plugin is-active wordpress-seo --url=% && echo "Plugin is activated on site %"'
@michael-sumner
michael-sumner / functions.php
Created September 5, 2023 15:23
Disable WordPress core attempts to redirect old posts.
<?php
// Disable WordPress core attempts to redirect old posts.
add_filter( 'old_slug_redirect_post_id', '__return_zero' );
add_filter( 'redirect_canonical', '__return_empty_string' );