Skip to content

Instantly share code, notes, and snippets.

@thomhines
thomhines / footer.php
Last active January 4, 2017 03:46
When selecting a theme via URL in Theme Test Drive (https://wordpress.org/plugins/theme-test-drive/) for WordPress, this script will make it so that all of your links will automatically get that theme name added to the link URL.
@thomhines
thomhines / change-wp-url-locations.php
Last active August 29, 2015 14:06
Quick script to alter WordPress database when moving a WordPress site from one URL to another.
<?php
// -------------------------------------------------
//
// WordPress URL Auto-Updater
// https://gist.github.com/thomhines/f3350204e6db2de32b7f
//
// -------------------------------------------------
@thomhines
thomhines / debounce.jquery.js
Created April 16, 2017 19:11
jQuery Debounce
var validation_debounce_timeout;
$.fn.debounce = function(callback, delay) {
$this = $(this);
clearTimeout(validation_debounce_timeout);
validation_debounce_timeout = setTimeout(function() {
callback();
}, delay);
@thomhines
thomhines / getValue.js
Created December 13, 2017 19:58
Better jQuery val() function - Returns value of all types of inputs, including radio buttons and checkboxes
jQuery.fn.getValue = function() {
if(this.is(':checkbox') && this.is(':checked')) return true;
else if(this.is(':checkbox') && !this.is(':checked')) return false;
else if(this.is(':radio') && $('input[name="'+this.attr('name')+'"]').filter(':checked').val()) return $('input[name="'+this.attr('name')+'"]').filter(':checked').val();
else if(this.is(':radio') && !$('input[name="'+this.attr('name')+'"]').filter(':checked').val()) return false;
else if(this.val()) return this.val();
return false;
}
@thomhines
thomhines / simpleswipe.jquery.js
Created December 13, 2017 22:14
Register a swipe gesture on touch screen. Requires jQuery.
var max_swipe_duration = 800;
var min_swipe_distance = 30;
var touchstartX, touchstartY, touchstartTime, touchMove;
// Record initial touch positions
$(window).on('touchstart', function(e) {
touchstartX = e.touches[0].screenX;
touchstartY = e.touches[0].screenY;
touchstartTime = new Date();
//
// inlineSVG
// Convert <img> elements pointing to SVG files to inline SVG
//
// With some help from https://snippetlib.com/jquery/replace_all_svg_images_with_inline_svg
jQuery.fn.extend({
inlineSVG: function(callback) {
$(this).each(function() {
var $img = $(this)
@thomhines
thomhines / example.js
Last active April 18, 2020 21:57
Capacitor iosMotion - Simple, partially-functional workaround for Capacitor Motion plugin (uses Native API instead of Web API). Place the .swift and .h files in the root of your App folder in Xcode, then add the javascript stuff to your app's code.
// Register the plugin with Capacitor
import { Plugins } from '@capacitor/core';
const { Keyboard } = Plugins;
// ORIENTATION
// Start tracking device orientation. This must be run in order for the following event listener to work
iosMotion.getOrientation()
@thomhines
thomhines / vue_sfc.sh
Last active February 14, 2021 19:07
Shell script to convert Vue single-file component files (.vue) into vanilla JS Vue components
# In shell, run `sh vue_sfc.sh inputfile.vue outputfile.js`
# HTML template and JS conversion only (doesn't do CSS), but it *can* do multiple components in one file.
# Note: you need to wrap your components in a non-standard tag `<component name="component_name">...</component>`
cat $1 | \
perl -00 -pe 's|\n\n|\n|gs' | \
perl -00 -pe 's|<component name="(.*?)">|Vue.component("\1", {\n\ttemplate:|gs' | \
perl -00 -pe 's|<template>(.*?)<\/template>|`\1`,|gs' | \
perl -00 -pe 's|<script>.*?{| |gs' | \
perl -00 -pe 's|<\/script>.*?</component>|);\n|gs' | \