Skip to content

Instantly share code, notes, and snippets.

View timkinnane's full-sized avatar

Tim Kinnane timkinnane

View GitHub Profile
@timkinnane
timkinnane / dynamic_image_downsize.php
Created February 2, 2015 08:49
Filter the output of image_downsize() to return dynamically generated images for intermediate or inline sizes. Long description in php doc block.
<?php
/**
* Filter the output of image_downsize() to return dynamically generated images for intermediate or inline sizes.
*
* <p>Because Wordpress generates all image sizes on first upload, if you change
* theme or size settings after the upload, there won't be a matching file for
* the requested size.<br/>
* This filter addresses the problem of the default downsize process laoding
* a large file and scaling it down in the browser if it doesn't find the right
* size image. This can cause large files to be loaded unnecessarily and will
@timkinnane
timkinnane / functions.php
Last active August 29, 2015 14:14 — forked from MikeNGarrett/wp-3-8-replace-open-sans
Replace WP dashboard and admin bar font with your own (optional) locally hosted font. NOTE: I've used Roboto to match my theme, but called it Open Sans. This prevents error for admin plugin dependencies that expect an Open Sans style to be registered. WHY: If I'm doing local development offline page loads were always delayed by the Google Fonts …
<?php
// Reregister Open-sans as Roboto.
function replace_open_sans() {
// Replace the URL with your local or remote css
$roboto_open_sans_url = get_template_directory_uri().'/assets/fonts/roboto-hack.css';
wp_deregister_style( 'open-sans' );
wp_register_style( 'open-sans', $roboto_open_sans_url );
}
add_action( 'admin_enqueue_scripts', 'replace_open_sans' );
@timkinnane
timkinnane / nested_custom_background_cb.php
Last active August 29, 2015 14:13
Apply custom background style on an element within the body. e.g. If you're using off-canvas and on-canvas containers, you may want the background only on the #canvas. It overwrites the theme support feature array value with a custom callback, but I haven't tested what happens if other key/values are defined elsewhere. There are no filters yet f…
function nested_custom_background_cb() {
ob_start();
_custom_background_cb();
$bg_style_tag = ob_get_contents();
ob_end_clean();
$bg_style_tag = str_replace( 'body.custom-background', 'body.custom-background #canvas', $bg_style_tag );
echo $bg_style_tag;
}
add_theme_support( 'custom-background', array( 'wp-head-callback' => 'nested_custom_background_cb' ) );
@timkinnane
timkinnane / change_passwd.sh
Last active August 29, 2015 14:09
Change .htpasswd setting (specifically on Digital Ocean droplets). Note This is a shell snippet but it can't be executed as is. Step 2 is manual.
# 1. Use htapasswd to set new pass for admin
sudo htpasswd /etc/apache2/.htpasswd admin
# 2. Update /etc/apache2/apache2.conf with folders to be protected
<DirectoryMatch ^.*/protected-folder/>
AuthType Basic
AuthName "Restricted Area"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</DirectoryMatch>
@timkinnane
timkinnane / Parent-sibling htaccess files
Last active August 29, 2015 14:06
htaccess for two Wordpress installs in side by side subfolders, one resolving to primary domain, one to subdomain.
## Example DOMAIN.TLD serves MAIN folder
## SUB folder served from SUB.DOMAIN.TLD
## --- public_html/ .htaccess --- ##
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?DOMAIN.TLD/$
RewriteRule ^$ /MAIN/ [L]
# Remove WWW (optional)
@timkinnane
timkinnane / Remove old uploads.sh
Last active December 22, 2015 04:48
Remove old uploads in gravity forms. e.g. Schedule as cron job after backups.
# find uploads > not web files > files only > older than 6 months
find ~/public_html/wp-content/uploads/gravity_forms/* -not -name "*.html" -not -name "*.php" -type f -mtime +182
# Find > delete
find ~/public_html/wp-content/uploads/gravity_forms/* -not -name "*.html" -not -name "*.php" -type f -mtime +182 -exec rm {} \;
@timkinnane
timkinnane / wpadmin_data_table.js
Last active January 25, 2017 10:36
dataTable takes an array of key/value objects and return table of elements. e.g from parsed JSON. Requires jQuery. Widefat class is used for Wordpress admin pages, but its not a WP specific function.
jQuery(document).ready(function($) {
function dataTable(data) {
var response_table = $('<table class="widefat"></table>');
var header = $('<thead><tr></tr></thead>');
for(var k in data[0]) {
header.append('<th>'+k+'</th>');
}
response_table.append(header);
@timkinnane
timkinnane / move_db.sh
Created June 21, 2013 01:09
SSH, export and move MySQL database to remote host
mkdir -p db_export_tmp
mysqldump --add-drop-table -efK -u DB_USER -pPASS DB_NAME > tmp/DB_NAME.sql
mysql -h host -u DB_USER -pPASS DB_NAME < tmp/DB_NAME.sql
rm -rf db_export_tmp
@timkinnane
timkinnane / rsync_public_html.sh
Created June 21, 2013 00:56
Rsync site to new host
# migrate from host to dest server - requires pass
rsync -zav --delete --progress public_html/ user@host:public_html/
@timkinnane
timkinnane / date_range_regex.php
Created April 7, 2013 23:37
Generate date range regex, PHP.
function get_daterange_regex( $from, $to, $input_format ) {
// get the difference in months
$date1 = DateTime::createFromFormat( $from, $input_format );
$date2 = DateTime::createFromFormat( $to, $input_format );
$interval = $date1->diff($date2);
$months = $interval->m;
// get an array of the interveening months
$all_months = array();