Skip to content

Instantly share code, notes, and snippets.

View fjarrett's full-sized avatar

Frankie Jarrett fjarrett

View GitHub Profile
@fjarrett
fjarrett / mu-plugin--wildcard-redirect-to-primary-host.php
Last active May 18, 2020 15:52
Wildcard redirect to primary host from alias domains
<?php
add_filter( 'init', function () {
if ( empty( $_SERVER['HTTP_AKAMAI_ORIGIN_HOP'] ) && empty( $_SERVER['HTTP_X_DSA_HOST'] ) && ! empty( $_SERVER['SERVER_NAME'] ) && 'origin-garage.godaddy.net' === $_SERVER['SERVER_NAME'] ) {
$path = ! empty( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '/garage';
wp_safe_redirect( 'https://www.godaddy.com' . $path, 301 );
@fjarrett
fjarrett / mu-plugin--block-robots-on-alias-domains.php
Last active May 18, 2020 15:48
Block robots from crawling alias domains
<?php
add_filter( 'option_blog_public', function ( $blog_public ) {
if ( empty( $_SERVER['HTTP_AKAMAI_ORIGIN_HOP'] ) && empty( $_SERVER['HTTP_X_DSA_HOST'] ) ) {
$blog_public = false;
}
@fjarrett
fjarrett / webhook-wp-cli.php
Last active May 10, 2022 15:25
Run a WP-CLI command via an authorized HTTP POST webhook
<?php
/**
* Run a WP-CLI command via an authorized HTTP POST webhook.
*
* curl -X POST https://mysite.com/webhook-wp-cli.php \
* -H 'Content-Type: application/json' \
* -H 'X-Authorization: Bearer MY_SUPER_SECRET_KEY_HERE' \
* -d '[ ["theme", "install"], ["https://downloads.wordpress.org/theme/go.zip"], ["force"], {"user": 1} ]'
*
@fjarrett
fjarrett / lock-down.php
Last active October 28, 2019 17:59
Simple must-use plugin to lock down WordPress and keep it up-to-date. Useful for canary sites.
<?php
/**
* Place this file in `wp-content/mu-plugins/lock-down.php` and it will be automatically loaded by WordPress.
*/
// Auto-update all the things.
add_filter( 'auto_update_core', '__return_true' );
add_filter( 'auto_update_plugin', '__return_true' );
add_filter( 'auto_update_theme', '__return_true' );
@fjarrett
fjarrett / wait-for-php-server-start.php
Created August 30, 2019 16:45
Use the Requests lib to wait for the built-in PHP server to start up
<?php
$process = new Process( sprintf( 'php -S localhost:8080 -t %s', __DIR__ ) );
$process->start();
$start = time();
// Wait for the server to start up (5 seconds max).
do {
try {
@fjarrett
fjarrett / .profile
Created August 10, 2017 15:55
Docker <--> Valet
# Toggle localhost between Docker and Valet
quitapp () {
command osascript -e 'quit app "'$*'"' &
}
alias usevalet="quitapp Kitematic && quitapp Docker && valet restart && mysqld &>/dev/null &"
alias usedocker="open -a Docker && valet stop && mysql.server stop && open -a Kitematic"
@fjarrett
fjarrett / cdn-baseurl.php
Created July 20, 2017 16:53
Filter the media uploads baseurl to point to a CDN
<?php
// Turn ON: $ wp option update cdn_baseurl https://abc123.cloudfront.net/wp-content/uploads
// Turn OFF: $ wp option delete cdn_baseurl
$cdn_baseurl = get_option( 'cdn_baseurl' );
if ( ! $cdn_baseurl ) {
return;
@fjarrett
fjarrett / php-constants-regex.md
Last active July 14, 2017 07:25
Match values of PHP constant definitions by type

The Regex

/define\(\s*[\'"]FOO[\'"]\s*,\s*(?:[\'"](.+)[\'"]|([\d]+)|([\d\.]+)|(true|false))\s*\)/gi

Purpose: To reference constant values from a PHP file (wp-config.php) that should not be included.
Author: Frankie Jarrett
Link: https://regex101.com/r/kBFIl5/

@fjarrett
fjarrett / gist:4ef5890043a80904c542339b1fd1ffb2
Created June 6, 2017 16:19
List of cache groups used in WordPress core
_meta
blog-details
blog-id-cache
blog-lookup
bookmark
calendar
category
comment
counts
networks
@fjarrett
fjarrett / centos.sh
Created June 5, 2017 15:55
Bash alias to allow root to run WP-CLI commands as another user
# force `wp` commands to run as the `www-data` user
wp() {
if [ $1 == "cli" ]; then
command wp $* --allow-root
elif [ $(whoami) != "www-data" ]; then
su www-data -c "wp $*"
else
command wp $*
fi
}