Skip to content

Collection of code snippets to make your life easier...

License

Notifications You must be signed in to change notification settings

ecotechie/bits-of-code

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 

Repository files navigation

Bits of Code

Collection of code snippets to make command line life a bit easier...

Table of Contents:

WordPress:

Enable updates when asked for FTP credentials

This happens when file/folder write permissions are set securely

Add this code to wp-config.php

From Stack Overflow

define('FS_METHOD', 'direct');

I love all you, precious plugins, slowing everything to a crawl, phoning home for fresh ads to show, so much.

From Twitter @Rarst

define( 'WP_HTTP_BLOCK_EXTERNAL', true );
define( 'WP_ACCESSIBLE_HOSTS', '*.wordpress.org' );

Stop WP embed script from loading

// Remove WP embed script
function ecotechie_stop_loading_wp_embed() {
    if ( ! is_admin() ) {
        wp_deregister_script( 'wp-embed' );
    }
}
add_action( 'init', 'ecotechie_stop_loading_wp_embed' );
/**
 * Conditionally change menus.
 */
function ecotechie_wp_nav_menu_args( $args = '' ) {
	// change the menu in the Header menu position.
	if ( 'top' === $args['theme_location'] && is_page( '138' ) ) {
		$args['menu'] = '4'; // 4 is the ID of the menu to use here
	}
	return $args;
}
add_filter( 'wp_nav_menu_args', 'ecotechie_wp_nav_menu_args' );

WP-CLI:

Get 20 largest autoloaded option values ordered by largest first

wp db query "SELECT option_name, length(option_value) AS option_value_length FROM wp_options WHERE autoload='yes' ORDER BY option_value_length DESC LIMIT 20;"

Get largest database tables in a "nice" format, excluding anything smaller than 1MB

wp db size --skip-plugins --format=csv --human-readable --all-tables | sed 's/ //g' | sort -k2 -h -t'\"' | grep KB -v | column -t -s, | sed 's/"//g'

Set database option to not autoload

wp db query "UPDATE $(wp db prefix)options SET autoload='no' WHERE option_name='OPTION_NAME'"

PHP:

Quick PHP debug with wp_die()

Replace ... with variables to display on screen

wp_die( '<pre>' . print_r( array( ... ), 1 ) . '</pre>' );

.htaccess:

Attempt to load files from production if they're not in our local version

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule wp-content/uploads/(.*) \
    http://{PROD}/wp-content/uploads/$1 [NC,L]
</IfModule>

Add www. and https

<IfModule mod_rewrite.c>
  RewriteCond %{HTTP_HOST} !^www\. [NC]
  RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  RewriteCond %{HTTP:X-Forwarded-Proto} !https
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

#not working
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP_HOST} !^www.
  RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

Strip www. add https

<IfModule mod_rewrite.c>
  RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
  RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
  RewriteCond %{HTTP:X-Forwarded-Proto} !https
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

#or using the hardcoded domain
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^www. [NC,OR]
  RewriteCond %{HTTPS} off
  RewriteRule ^(.*)$ https://DOMAIN.com/$1 [L,R=301]
<IfModule>

#not working
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
  RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [L,R=301]
<IfModule>

Bash Commands:

Generate public/private rsa key pair with (YOUR_EMAIL) as note, then output keys to screen

ssh-keygen -C YOUR_EMAIL -m PEM -f ./id_rsa && echo && cat id_rsa && echo && cat id_rsa.pub

SSH tunnel into a MySQL shell:

ssh -i ./id_rsa -p 22 [email protected] -L 65000:127.0.0.1:3306 -N

Display new information as it is being written to a file (FILE_NAME)

tail --follow FILE_NAME

Display disk space information

df -hT

List directories sorted by total number of files

du -a | cut -d/ -f2 | sort | uniq -c | sort -n

List directories sorted by total number of files recursively, starting with (.)

From StackExchange

find . -type d | while read dir; do echo "$dir" : $(find "$dir" -type f | wc -l); done | sort -k2 -t ':' -n;

List directories and files sorted by size

From StackOverflow

du --all --apparent-size --human-readable --max-depth=1 2>/dev/null | sort -h

Show total size of files of type (.jpg) recursively in a directory (.)

From StackExchange

find . -type f -name '*.jpg' -exec du --apparent-size --human-readable --total {} + | grep total$

Show total size of a directory (.)

From StackExchange

du --summarize --human-readable --apparent-size .

TTL value of a DNS record

From ShellHacks

Remaining

dig +noall +answer domain.com

Configured

DOMAIN=domain.com; dig +noall +answer $DOMAIN @$(dig NS $DOMAIN +short|head -n1)

Website response time

From ShellHacks

curl -s -w '\nLookup time:\t%{time_namelookup}\nConnect time:\t%{time_connect}\nAppCon time:\t%{time_appconnect}\nRedirect time:\t%{time_redirect}\nPreXfer time:\t%{time_pretransfer}\nStartXfer time:\t%{time_starttransfer}\n\nTotal time:\t%{time_total}\n' -o /dev/null domain.com

Print the public IP

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

Compress PDF

ps2pdf in.pdf out.pdf

Search recursesively

Files modified on Jan 11 at 4am

find . -type f -ls | grep 'Jan 11 04'

Files edited in date range

find . -type f -newermt 2018-01-10 ! -newermt 2018-01-11

chmod recursevly

CAREFUL WITH THESE!!!

f = files, d = directories

find . -type f ! -perm 0644 -print0 | xargs -0 chmod 644
find . -type d ! -perm 755 -print0 | xargs -0 chmod 755

Find files and directories not owned by GROUPNAME, excluding some paths

find . ! -group GROUPNAME ! -path './APATH/' ! -path './ANOTHERPATH'

Bash Scripting:

Qick and dirty timer

From Unix Stack Exchange

start=`date +%s`
# Code to be timed...
end=`date +%s`

runtime=$((end-start))

or

start_time=$SECONDS
# Code to be timed...
elapsed_time=$(($SECONDS - $start_time))

“Validate Input” Function

Inspired by Tec Admin

validate_input () {
  read -r -p "$*" input 
  while true
  do
    read -r -p "Is $input correct? [Y/n] " maybe

    case $maybe in
      [yY][eE][sS]|[yY]|"")
      echo "$input"
      break
      ;;
      [nN][oO]|[nN])
      read -p "Enter variable again:" $input
      ;;
      *)
      echo "Invalid input..."
      echo
      ;;
    esac
  done
}

"Yes/No" Function

yes_no () {
  echo
  while true
  do
    read -r -p "$* [Y/n] " maybe
    
    case $maybe in
      #Return 0, a non failure status.
      [yY][eE][sS]|[yY]|"")
      return 0
      break
      ;;
      # Return 101, a non-zero (failure) status.
      [nN][oO]|[nN])
      return 101
      break
      ;;
      *)
      echo "Invalid input..."
      ;;
    esac
  done
}

Quick Yes/No logic

read -r -p "What's your question? [y/N] " response
[[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]] && $do_something || $do_something_else

About

Collection of code snippets to make your life easier...

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published