Skip to content

Instantly share code, notes, and snippets.

Avatar
🤓

Gerben van Dijk gerbenvandijk

🤓
  • Tech Lead Frontend @ Tui Spain
  • Amsterdam, The Netherlands
View GitHub Profile
View Detect safari on windows
safari = ($.browser.webkit && !(/chrome/.test(navigator.userAgent.toLowerCase())));
windows = (navigator.userAgent.indexOf("Win")!=-1);
if (safari && windows) {
alert('this is safari on windows');
}
@gerbenvandijk
gerbenvandijk / Mark parent navigation active when on custom post type single page
Last active Jul 12, 2022
Mark (highlight) custom post type parent as active item in Wordpress Navigation.When you visit a custom post type's single page, the parent menu item (the post type archive) isn't marked as active. This code solves it by comparing the slug of the current post type with the navigation items, and adds a class accordingly.
View Mark parent navigation active when on custom post type single page
<?php
function add_current_nav_class($classes, $item) {
// Getting the current post details
global $post;
// Get post ID, if nothing found set to NULL
$id = ( isset( $post->ID ) ? get_the_ID() : NULL );
@gerbenvandijk
gerbenvandijk / random video ID from a users videofeed
Last active Dec 17, 2015
// Function that returns random video ID from a users videofeed
View random video ID from a users videofeed
<?php
// Function that returns random video ID from a users videofeed
function RandomYoutube($youtubefeed){
$feedresult = simplexml_load_file($youtubefeed);
$videoids = array();
foreach ($feedresult->entry as $video) {
@gerbenvandijk
gerbenvandijk / ShowTweets.php
Last active Dec 18, 2015
Get tweets (using the Twitter API 1.1) and cache them using the Wordpress Transient API. You need twitter-api-php (http://github.com/j7mbo/twitter-api-php).
View ShowTweets.php
<?php
function ShowTweets(){
// Setting the name of our transient
$transName = 'twitter';
// Time that the transient expires (in minutes)
$cacheTime = 10;
@gerbenvandijk
gerbenvandijk / WordpressShortContent.php
Last active Dec 18, 2015
Function to display a Wordpress string (e.g. post title or content) with a max number of characters.
View WordpressShortContent.php
<?php
function short_content($content,$limit) {
$content = strip_tags(html_entity_decode($content, ENT_QUOTES, "UTF-8"));
// when the content is longer then $limit, show three dots at the end
if(strlen($content) >= ($limit+3)) {
$content = substr($content, 0, $limit) . '...';
@gerbenvandijk
gerbenvandijk / ValidImg.js
Created Sep 27, 2013
Check for a valid image in HREF.
View ValidImg.js
function IsValidImageUrl(url, callback) {
jQuery("<img>", {
src: url,
error: function() { callback(false); },
load: function() { callback(true); }
});
@gerbenvandijk
gerbenvandijk / clickToggle.js
Created Nov 19, 2013
jQuery function to toggle a click and run custom code on them (.toggle() always shows or hides an element, so we can't use this anymore).
View clickToggle.js
(function($) {
$.fn.clickToggle = function(func1, func2) {
var funcs = [func1, func2];
this.data('toggleclicked', 0);
this.click(function() {
var data = $(this).data();
var tc = data.toggleclicked;
$.proxy(funcs[tc], this)();
data.toggleclicked = (tc + 1) % 2;
});
@gerbenvandijk
gerbenvandijk / OldBrowser.js
Created Nov 19, 2013
Detect if the user has an old browser and if so, it inserts a friendly notification that not all features on the site will work, and that an upgrade is recommended.
View OldBrowser.js
// The code - first bit is all the UserAgent library.
(function(wndw) {
var Browsers, OS, Platform, Versions, browser_name, browser_version, os, platform;
Versions = {
Firefox: /firefox\/([\d\w\.\-]+)/i,
IE: /msie\s([\d\.]+[\d])/i,
Chrome: /chrome\/([\d\w\.\-]+)/i,
Safari: /version\/([\d\w\.\-]+)/i,
Ps3: /([\d\w\.\-]+)\)\s*$/i,
Psp: /([\d\w\.\-]+)\)?\s*$/i
@gerbenvandijk
gerbenvandijk / StickyScroll.js
Created Nov 19, 2013
Simple jQuery script to make part(s) of the site sticky when scrolled beyond a certain point.
View StickyScroll.js
// sticky scroll function
function ScrollNav() {
if ($(document).scrollTop() > 0) {
} else {
}
}
View RGBtoHEX.js
// Function to convert RGB Color to hex
function rgbToHex(r, g, b) {
if (r > 255 || g > 255 || b > 255)
throw "Invalid color component";
return ((r << 16) | (g << 8) | b).toString(16);
}