Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / .htaccess
Last active September 5, 2024 18:28
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>