Wordpress Common Configs & Commands

WordPress Common Configs & Commands

.htaccess File

Default WordPress .htaccess file

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Default WordPress .htaccess file with AutoSSL exclusions

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(\.well-known|\.well-known/.*)$ 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

WordPress in a subfolder

# BEGIN WordPress

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/my_subdir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /my_subdir/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ my_subdir/index.php [L] 
# END WordPress

Reinstall WordPress core files

current=$(wp core version);read -p "What WordPress version [$current]? " version; if [[ $version = "" ]]; then version=$current; fi; echo "$version"; mkdir extract; backup="../old-wp-core.$(date +%B.%d.%Y.%s)"; cd extract; wget https://wordpress.org/wordpress-$version.tar.gz; mkdir $backup; less wordpress-$version.tar.gz | grep -v '.*/$' | awk '{print $6}' | cut -c 11- | while read file; do dir=$(echo $file  | grep '/' | sed 's~/[^/]*$~~'); if [ ! -z "$dir" ] ; then mkdir -p $backup/$dir 2>/dev/null; fi; mv ../$file $backup/$file; done; tar -xf wordpress-$version.tar.gz;/bin/cp -afr wordpress/* ..; cd ..; rm -rf extract; rm -f wordpress-$version.tar.gz; echo; echo Done; echo;

List active theme in database (useful when it’s not installed)

wp db query "SELECT * FROM wp_options WHERE option_name = 'template'"

List active plugins in database (useful when they’re not installed)

wp db query "SELECT * FROM wp_options WHERE option_name = 'active_plugins'";

Reset login attempts

wp db query "UPDATE wp_options SET option_value = '' WHERE option_name = 'limit_login_lockouts' LIMIT 1"

List active plugins (useful for scripts)

wp plugin list --status=active --field=name

This is needed in the .htaccess file when the user is getting an HTTP error when trying to upload large images.

SetEnv MAGICK_THREAD_LIMIT 1

I needed this line to only redirect for htaccess-logged-in users. I needed this once when a site was loading site content after clicking cancel on login prompt.

RewriteCond %{REMOTE_USER} ^[a-z].*

Update all WordPress installations on a server

cd /home/;find -name wp-config.php | while read in; do echo "Checking $in";su - $(echo "$in" | sed -r 's-^\./([a-zA-Z0-9]*)(/.*)-\1-') -c "cd $(echo "$in" | sed -r -e 's-^\./[a-zA-Z0-9]*/--' -e 's,/wp-config.php$,,');wp core update;"; done

Install The Theme Listed in the Database

wp theme install $(wp db query "SELECT option_value FROM $(cat wp-config.php | sed -nr "s/(\\\$table_prefix)([ ]*?)=([ ]*?)([\"'])([A-Za-z0-9_]*)([\"'])(.*?)/\5/p")options WHERE option_name='current_theme' OR option_name='template'" --skip-plugins | sed -e '' | head -2 | tail -1) --skip-plugins

Reinstall Active Plugins

This will not install active missing plugins.

wp plugin install $(wp plugin list --field=name --skip-plugins) --force --skip-plugins

Little Commands Useful For Scripts

Get Clean DB User (no PHP)

sed -nr -e "s~//(.*?)$~~" -e "/DB_USER/ s/^(.*)DB_USER[\"'].*,[ \t]*?[\"']([^'\"]*)[\"']([ \t]*?)\).*$/\2/p" wp-config.php

Get Clean DB Name (no PHP)

sed -nr -e "s~//(.*?)$~~" -e "/DB_NAME/ s/^(.*)DB_NAME[\"'].*,[ \t]*?[\"']([^'\"]*)[\"']([ \t]*?)\).*$/\2/p" wp-config.php

Get Clean DB Password (no PHP)

sed -nr -e "s~//(.*?)$~~" -e "/DB_PASSWORD/ s/^(.*)DB_PASSWORD[\"'].*,[ \t]*?[\"']([^'\"]*)[\"']([ \t]*?)\).*$/\2/p" wp-config.php

Get Clean DB Prefix (no PHP)

sed -nr "s/\\\$table_prefix[ ]*?=[ ]*?[\"']([A-Za-z0-9_]*)[\"'].*?/\1/p" wp-config.php

Read the Database Info from a Config File

egrep 'DB_HOST|WP_ALLOW_MULTISITE|DB_NAME|DB_USER|DB_PASSWORD|table_prefix' wp-config.php

Login with wp-config.php credentials without using php

Terminal Command

mysql $(sed -nr -e "s~//(.*?)$~~" -e "/DB_NAME/ s/^(.*)DB_NAME[\"'].*,[ \t]*?[\"']([^'\"]*)[\"']([ \t]*?)\).*$/\2/p" wp-config.php) -u $(sed -nr -e "s~//(.*?)$~~" -e "/DB_USER/ s/^(.*)DB_USER[\"'].*,[ \t]*?[\"']([^'\"]*)[\"']([ \t]*?)\).*$/\2/p" wp-config.php) --password=$(sed -nr -e "s~//(.*?)$~~" -e "/DB_PASSWORD/ s/^(.*)DB_PASSWORD[\"'].*,[ \t]*?[\"']([^'\"]*)[\"']([ \t]*?)\).*$/\2/p" wp-config.php)

Fix Zip Uploads

add_filter('upload_mimes', 'custom_upload_mimes');
function custom_upload_mimes ( $existing_mimes=array() ) {
    // add your extension to the mimes array as below
    $existing_mimes['zip'] = 'application/zip';
    $existing_mimes['gz'] = 'application/x-gzip';
    return $existing_mimes;
}

Allow Zip Uploads

add_filter('upload_mimes', 'custom_upload_mimes');
function custom_upload_mimes ( $existing_mimes=array() ) {
 // add your extension to the mimes array as below
 $existing_mimes['zip'] = 'application/zip';
 $existing_mimes['gz'] = 'application/x-gzip';
 return $existing_mimes;
} 

Make All-in-one WP Migration take large backup in the free version

https://ryankozak.com/all-in-one-migration-sizelimit/