Skip to content

Instantly share code, notes, and snippets.

View rmpel's full-sized avatar

Remon Pel rmpel

View GitHub Profile
@rmpel
rmpel / scroll-depth-tracking.js
Last active October 19, 2018 06:43
Send event to Google Analytics on scrolling past certain points on a page
(function($, w, d) {
function autoDetectGA() {
if (typeof _gaq !== 'undefined') {
return 'ga';
}
if (typeof ga === 'function') {
return 'ua';
}
if (typeof dataLayer !== 'undefined') {
return 'gtm';
@rmpel
rmpel / jQuery.filter-outbound.js
Last active January 20, 2021 11:51
Filter outbound links, external images, scripts and stylesheets with jQuery(":outbound");
(function($){
$.expr[':'].outbound = function(elem, index, match){
var att = 'nothing';
if ($(elem).is('a') || $(elem).is('link')) {
att = 'href';
}
if ($(elem).is('img') || $(elem).is('script')) {
att = 'src';
}
@rmpel
rmpel / jQuery.append-to-attribute.js
Created September 5, 2016 09:26
Append to attribute (jQuery)
(function($){
$.fn.appendAttr = function(attr, value, cat) {
var cat = cat || ",";
return $(this).each(function(){
var a = $(this).attr(attr) || "";
if (a) a = a + cat;
a = a + value;
$(this).attr(attr, a);
});
}
@rmpel
rmpel / ga-event-pageview.js
Last active September 5, 2016 10:32
Always-working Google Analytics Event/Pageview code
(function () {
/**
* Track a virtual pageview
* @param url String the URL to send to GA. Developer will need to prefix as needed. /virtual/ for virtual pages, /download/ for downloads are a good startingpoint.
* @param title String (optional) Title to send with the page-hit. document.title will be used if omitted.
* @returns {Number} the result of the ga(), _gaq.push() or pageTracker._trackPageview call
*/
window.gaPageview = function (url, title) {
title = title || false;
if (!title) {
@rmpel
rmpel / gist:3c882d59a28570b10df9fe0ee2f72dac
Last active September 6, 2016 11:46 — forked from padolsey/gist:527683
IE Version Detect
// ----------------------------------------------------------
// A short snippet for detecting versions of IE in JavaScript
// without resorting to user-agent sniffing
// ----------------------------------------------------------
// If you're not in IE (or IE version is less than 5) then:
// ie === undefined
// If you're in IE (>=5) then you can determine which version:
// ie === 7; // IE7
// Thus, to detect IE:
// if (ie) {}
@rmpel
rmpel / scrollIntoView.js
Last active September 14, 2016 12:50
Scroll an element into view
(function($){
$.fn.scrollIntoView = function(speed,callFunc) {
var that = this;
if ($(this).length > 0) {
setTimeout(function() {
var targetOffset = $(that).offset().top - 0.2 * $(window).innerHeight();
$("html,body").animate({scrollTop: targetOffset}, speed || 500, callFunc);
},500);
}
return $(this);
@rmpel
rmpel / convert_encoding.sql
Created October 13, 2016 15:44
UTF-8 Recoding in MySQL
UPDATE wp_posts SET post_title = CONVERT(CAST(CONVERT(post_title USING latin1) AS BINARY) USING utf8)
@rmpel
rmpel / learn-spam.sh
Created October 24, 2016 13:23
Mass-Spam-learning for SpamAssassin
#!/usr/bin/env bash
if [ ! -f ~/.learnspamlock ]; then
touch ~/.learnspamlock
IFS=$'\n'
for i in ` find ./* | egrep "(Junk|spam)" | egrep "(cur|new)$" `; do
CNT=$(sa-learn --spam "$i" | egrep -o "from ([0-9]+)" | egrep -o "([0-9]+)" )
if [ $CNT -gt 0 ]; then
echo $CNT spam messages trained in $i
else
@rmpel
rmpel / diacritics-restore.sql
Last active March 13, 2018 16:15
Repair diacritics encoding in MySQL database. Replace table-name and field-name with UTF-8 capable editor (Sublime Text 3 is verified to not mess up the query)
update wp_posts set
post_content = replace(post_content, CONVERT(CAST(CONVERT('¡' USING utf8) AS BINARY) USING latin1), '¡'),
post_content = replace(post_content, CONVERT(CAST(CONVERT('¢' USING utf8) AS BINARY) USING latin1), '¢'),
post_content = replace(post_content, CONVERT(CAST(CONVERT('£' USING utf8) AS BINARY) USING latin1), '£'),
post_content = replace(post_content, CONVERT(CAST(CONVERT('¤' USING utf8) AS BINARY) USING latin1), '¤'),
post_content = replace(post_content, CONVERT(CAST(CONVERT('¥' USING utf8) AS BINARY) USING latin1), '¥'),
post_content = replace(post_content, CONVERT(CAST(CONVERT('¦' USING utf8) AS BINARY) USING latin1), '¦'),
post_content = replace(post_content, CONVERT(CAST(CONVERT('§' USING utf8) AS BINARY) USING latin1), '§'),
post_content = replace(post_content, CONVERT(CAST(CONVERT('¨' USING utf8) AS BINARY) USING latin1), '¨'),
post_content = replace(post_content, CONVERT(CAST(CONVERT('©' USING utf8) AS BINARY) USING latin1), '©'),
@rmpel
rmpel / multilingual-search-slug-with-wpml.php
Last active July 7, 2022 10:09
WPML Multilingual Search slug
<?php
/*-----------------------------------------
REWRITE SEARCH URLS
-----------------------------------------*/
// Redirect ?s=xxx to /search/xxx
function redirect_search_url()
{
if (is_search() && !empty($_GET['s'])) {
wp_redirect(home_url('/' . _x('search' /* default search slug */, 'search url slug', THEME_TEXT_DOMAIN) . '/') . urlencode(get_query_var('s')));