Skip to content

Instantly share code, notes, and snippets.

View verticalgrain's full-sized avatar

Jamie Graham verticalgrain

View GitHub Profile
@verticalgrain
verticalgrain / formatdecimals.js
Created May 17, 2016 16:59
Format decimal places of number in js
// If a number is a whole number, return the number
// If a number is a decimal, return the number to two decimal places
function formatDecimals(number) {
if(Math.round(number) !== number) {
return number.toFixed(2);
} else {
return number;
}
}
@verticalgrain
verticalgrain / insertAtCaret.js
Last active September 4, 2017 17:54
Insert text at the current caret position of the textarea field that currently has focus
// Usage:
// insertAtCaret('some text to insert');
(function ($, undefined) {
function insertAtCaret(text) {
var $focused = $(':focus');
var focusedCursorPosition = $focused.getCursorPosition();
var focusedContent = $focused.val();
var focusedNewContent = focusedContent.substr(0, focusedCursorPosition) + ' ' + string + focusedContent.substr(focusedCursorPosition);
@verticalgrain
verticalgrain / increase-upload-size-limit.php
Created November 3, 2016 23:08
Increase upload size limit wordpress hook
<?php
add_filter('upload_size_limit', 'd7_increase_upload');
function d7_increase_upload() {
// 250mb
return '262144000';
}
@verticalgrain
verticalgrain / share-links.twig
Last active June 25, 2020 09:49
Timber twig wordpress static sharing links
@verticalgrain
verticalgrain / 0_reuse_code.js
Created December 2, 2016 17:53
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@verticalgrain
verticalgrain / srcset.php
Created December 20, 2016 18:46
Wordpress srcset snippet for ACF image field
<?php
// Use an ACF image field
// Set the 'return value' option to "array" (this is the default)
// This example uses three image sizes, called medium, medium_large, thumbnail
$imageobject = get_field('image');
if( !empty($imageobject) ):
echo '<img alt="' . $imageobject['title'] . '" src="' . $imageobject['sizes']['medium'] . '" srcset="' . $imageobject['sizes']['medium_large'] .' '. $imageobject['sizes']['medium_large-width'] .'w, '. $imageobject['sizes']['medium'] .' '. $imageobject['sizes']['medium-width'] .'w, '. $imageobject['sizes']['thumbnail'] .' '. $imageobject['sizes']['thumbnail-width'] .'w">';
endif;
?>
@verticalgrain
verticalgrain / jqxhr-debugging.js
Created December 22, 2016 23:19
jqXHR success and error logging example for $.get
var request = $.get('http://wp-recipes/wp-json/wp/v2/posts', {}, function(data, textStatus, jqXHR){
// Do some stuff
}, 'json');
request.success(function(result) {
console.log('success:');
console.log(result);
});
@verticalgrain
verticalgrain / video-end-function.js
Created January 5, 2017 17:31
Manipulate HTML5 video at X seconds from end of video
// Dynamically get the video length, and do something to a video X seconds before it ends
$('.video').on('timeupdate', function(event) {
var timeFromVideoEnd = 2000;
var current = Math.round(event.target.currentTime * 1000);
var total = Math.round(event.target.duration * 1000);
if ( ( total - current ) < timeFromVideoEnd ) {
$body.addClass('fade-video-out');
}
});
@verticalgrain
verticalgrain / smooth-scroll.js
Created January 6, 2017 23:26
jQuery smooth scroll anchor link script - one and done
// One and done smooth scrolling for anchor links
// Taken from: https://css-tricks.com/snippets/jquery/smooth-scrolling/
$(function() {
$('a[href*="#"]:not([href="#"])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html, body').animate({
scrollTop: target.offset().top
@verticalgrain
verticalgrain / image-srcset.twig
Created January 9, 2017 18:53
Timber + Twig srcset image partial
{# Include in views like so: {% include 'partials/image-srcset.twig' with {image_id: post.get_field('hero_background_image'), class: 'hero__background'} %} #}
{# To print image object: {{post.get_field('hero_background_image')|print_r}} #}
<img class="{{class}}" alt="{{TimberImage(image_id).alt}}"
src="{{ TimberImage(image_id).src('large') }}"
srcset="{{ TimberImage(image_id).src('xlarge') }} {{ image_id['sizes']['xlarge-width'] }}w,
{{ TimberImage(image_id).src('hero') }} {{ image_id['sizes']['hero-width'] }}w,
{{ TimberImage(image_id).src('large') }} {{ image_id['sizes']['large-width'] }}w
" />