Skip to content

Instantly share code, notes, and snippets.

@jacobovidal
jacobovidal / .htaccess
Last active July 2, 2023 22:31
Upgrade Insecure Requests via .htaccess or meta tag to prevent mixed content
<ifModule mod_headers.c>
Header always set Content-Security-Policy "upgrade-insecure-requests;"
</IfModule>
@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 / 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 / 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 / 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 / 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 / scroll-tracking.js
Last active December 30, 2018 07:42
Google Analytics scroll tracking snippet using dataLayer
// https://github.com/felix/gtm-scrolldepth
var startTime = +new Date;
var scrollCache = [];
documentElement = document.documentElement;
function throttle(func, wait) {
var context, args, result;
var timeout = null;
var previous = 0;
var later = function () {
@jacobovidal
jacobovidal / .htaccess
Created July 27, 2017 15:46
Security headers to .htaccess
<ifModule mod_headers.c>
Header always set X-Content-Type-Options "nosniff"
Header always set X-XSS-Protection "1; mode=block"
Header always set X-Frame-Options "deny"
</IfModule>
@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];