Skip to content

Instantly share code, notes, and snippets.

@jacobovidal
jacobovidal / service-worker.js
Created July 12, 2017 11:07
Install Service Worker and cache new requests cumulatively (Only GET method)
var CACHE_NAME = 'example-cache-v1';
var urlsToCache = [];
self.addEventListener('install', function(event) {
// Perform install steps
event.waitUntil(
caches.open(CACHE_NAME)
.then(function(cache) {
console.log('Opened cache');
return cache.addAll(urlsToCache);
@jacobovidal
jacobovidal / set-and-get-cookies.js
Last active July 12, 2017 18:30
Set and get cookies function in JavaScript
/**
* @param {string} cname - Cookie name
* @param {string} cvalue - Cookie value
* @param {int} exdays - Cookie expiration time in days
*/
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var path = "path=/",
expires = "expires=" + d.toUTCString();
@jacobovidal
jacobovidal / get-parameters-by-name.js
Last active July 12, 2017 18:31
Get URL parameters by name in JavaScript
/**
* @param {string} name - Parameter name
* @param {string} url - URL to extract parameters from (Optional)
*/
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
@jacobovidal
jacobovidal / httpd.conf
Last active July 12, 2017 18:33
Add this snippet to wp-config.php to set up HTTPS/SSL in WordPress behind a proxy
<VirtualHost *:443>
# ...
RequestHeader set X-Forwarded-Proto "https"
RequestHeader set X-Forwarded-Port "443"
# ...
</VirtualHost>
@jacobovidal
jacobovidal / upgrade-url.sql
Last active July 31, 2017 14:45
Change and update WordPress URL after migration or moving to a new host
SET @oldurl='http://www.oldurl.com';
SET @newurl='http://www.newurl.com';
UPDATE wp_options SET option_value = replace(option_value, @oldurl, @newurl) WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, @oldurl, @newurl);
UPDATE wp_posts SET post_content = replace(post_content, @oldurl, @newurl);
UPDATE wp_postmeta SET meta_value = replace(meta_value, @oldurl, @newurl);
@jacobovidal
jacobovidal / slugify.js
Last active August 30, 2017 09:34
JavaScript slugify
function slugify(text)
{
var accents = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž';
var accentsOut = "AAAAAAaaaaaaOOOOOOOooooooEEEEeeeeeCcDIIIIiiiiUUUUuuuuNnSsYyyZz";
text = text.split('');
var txtLen = text.length;
var i, x;
for (i = 0; i < txtLen; i++) {
if ((x = accents.indexOf(text[i])) != -1) {
text[i] = accentsOut[x];
@jacobovidal
jacobovidal / observer-example.js
Created November 9, 2017 15:39
Mutation observer example in JavaScript
if (window.MutationObserver || window.WebKitMutationObserver) {
var observer = new MutationObserver(function (mutations, observer) {
// Do something after node has changed
console.log('#div has changed')
});
// Configuration of the observer
var config = {
subtree: true,
attributes: true
@jacobovidal
jacobovidal / functions.php
Created July 12, 2017 12:19
Disable default tags, emojis and resources in WordPress via functions.php
<?php
/**
* Disable Emojis
*/
remove_action('admin_print_styles', 'print_emoji_styles');
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('admin_print_scripts', 'print_emoji_detection_script');
remove_action('wp_print_styles', 'print_emoji_styles');
remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
remove_filter('the_content_feed', 'wp_staticize_emoji');
@jacobovidal
jacobovidal / in_viewport.js
Created December 18, 2017 01:36
Check if element is in viewport
// Determine if an element is in the visible viewport
function isInViewport(element) {
var rect = element.getBoundingClientRect();
var html = document.documentElement;
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || html.clientHeight) &&
rect.right <= (window.innerWidth || html.clientWidth)
);
@jacobovidal
jacobovidal / isScrollable.js
Created February 7, 2018 09:55
Scroll tracking trigger to check if page height is bigger than the double of your viewport (Useful for Google Tag Manager)
// If using with GTM, anonymize the function name
function isScrollable() {
function getDocumentHeight() {
return Math.max(document.body.scrollHeight, document.body.offsetHeight,
document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);
}
function getViewPortHeight() {
return Math.max(document.documentElement.clientHeight, window.innerHeight || 0) * 2;
}