Skip to content

Instantly share code, notes, and snippets.

View tott's full-sized avatar

Thorsten Ott tott

  • Germany, Cologne area
View GitHub Profile
@tott
tott / frontend-force-mapped-domain.php
Created November 14, 2013 14:14
Enforce domain mapping mapped domain for all front-end urls ran through esc_url()
add_filter( 'clean_url', 'sav_force_mapped_url' );
function sav_force_mapped_url( $url ) {
if ( ! is_admin() ) {
if ( function_exists( 'get_original_url' ) && strpos( '/wp-admin', $url ) === false ) {
$original = preg_replace( '#^https?://#', '', get_original_url('') );
$new = preg_replace( '#^https?://#', '', site_url() );
$url = str_ireplace( $original, $new, $url );
}
}
return $url;
@tott
tott / functions.php
Created November 15, 2013 08:49
validate if a path is a subfolder of WP_CONTENT_DIR
/**
* Validate that a given path is a child of the WP_CONTENT_DIR
* @param string $path path to check
* @return boolean true/false
*/
function sav_path_is_valid( $path ) {
if ( !defined( WP_CONTENT_DIR ) )
return false;
$start = 0;
@tott
tott / compare-urls.sh
Created December 10, 2013 11:18
Compare html served by different servers. This script will first get the page from the source ip and extract a list of links on this output. then it will compare the output of each link for both servers.
#!/bin/bash
ip1='FILL-SOURCE-IP'; ip2='FILL-DEST-IP'; host='FILL-HOSTNAME'; curl http://$host | grep -o '<a.*href=.*>' | grep "http://$host" | sed -e 's/<a .*href=['"'"'"]//' -e 's/["'"'"'].*$//' -e '/^$/ d' > links.txt; for link in `cat links.txt`; do path=`echo -n $link | cut -d "/" -f 4-`; curl --header "Host: $host" "http://$ip1/$path" > ip1.html; curl --header "Host: $host" "http://$ip2/$path" > ip2.html; echo; echo "Diff http://$ip1/$path"; echo; diff ip1.html ip2.html; done
@tott
tott / gist:7908001
Created December 11, 2013 10:11
Get a space separated list of all hosts in an apache config so you can add them to your hosts file
#!/bin/bash
grep -E "Server(Name|Alias)" httpd.conf | sort | uniq | cut -d " " -f 6 | tr "\n" " "
@tott
tott / gist:8517558
Created January 20, 2014 09:49
indexOf was added to the ECMA-262 standard in the 5th edition; as such it may not be present in all browsers. You can work around this by utilizing the following code at the beginning of your scripts. This will allow you to use indexOf when there is still no native support. This algorithm matches the one specified in ECMA-262, 5th edition, assum…
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (searchElement, fromIndex) {
if ( this === undefined || this === null ) {
throw new TypeError( '"this" is null or not defined' );
}
var length = this.length >>> 0; // Hack to convert object.length to a UInt32
fromIndex = +fromIndex || 0;
@tott
tott / gist:8639621
Created January 26, 2014 21:17
Speed up wp-admin when there are a lot of terms
add_action( 'admin_init', 'deregister_autosuggest' );
function deregister_autosuggest() {
if ( is_admin() ) {
wp_deregister_script( 'suggest' );
}
}
@tott
tott / alter-robots-txt.php
Created November 14, 2013 14:15
alter robots.txt
add_filter( 'robots_txt', 'sav_add_sitemap_to_robots' );
function sav_add_sitemap_to_robots( $robots ) {
$robots .= 'Sitemap: ' . site_url( 'sitemap_index.xml' );
return $robots;
}
#!/bin/bash
# ./curl-urls.sh numprocessses http://urltograp urlpattern
# ./curl-urls.sh 3 http://www.domain.com domain.com
numprocessses=$1
baseurl=$2
urlpattern=$3
function forky() {
local num_par_procs
if [[ -z $1 ]] ; then
@tott
tott / gist:0cc78e7dcad18024d1c8
Created July 1, 2014 20:35
Create a full list of sites with domain mapping with wp-cli
for i in `wp site list | cut -f 2 | grep -v url`; do wp --url=$i eval 'global $wpdb, $blog_id; $blogname=get_option("blogname"); $blogurl=home_url(); $domains=$wpdb->get_results( "SELECT * FROM {$wpdb->dmtable} WHERE blog_id = $blog_id;" ); foreach( $domains as $data ) { echo $blog_id.",".$blogname.",".$blogurl.",".$data->domain.",".$data->active."\n"; }'; done
@tott
tott / secure-auth-cookies.php
Last active February 17, 2017 15:17
Encrypt WordPress auth cookies
<?php
function sav_encrypt_cookie( $decrypted ) {
$encrypted = mcrypt_encrypt( MCRYPT_RIJNDAEL_256, substr( AUTH_SALT, 0, 32 ), $decrypted, MCRYPT_MODE_ECB, mcrypt_create_iv( mcrypt_get_iv_size( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB ), MCRYPT_RAND ) );
return trim( base64_encode( $encrypted ) );
}
function sav_decrypt_cookie( $encrypted ) {
$decrypted = mcrypt_decrypt( MCRYPT_RIJNDAEL_256, substr( AUTH_SALT, 0, 32 ), base64_decode( $encrypted ), MCRYPT_MODE_ECB, mcrypt_create_iv( mcrypt_get_iv_size( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB ), MCRYPT_RAND ) );
return trim( $decrypted );