Skip to content

Instantly share code, notes, and snippets.

View MichaelVanDenBerg's full-sized avatar
🏠
Working from home

Michael van den Berg MichaelVanDenBerg

🏠
Working from home
View GitHub Profile
@MichaelVanDenBerg
MichaelVanDenBerg / detect-scroll.js
Last active December 7, 2015 18:12
Detect scroll distance from top and/or bottom and scroll up and down.
/**
* Detect Scroll.
*
* Detect scroll distance from top and/or bottom and scroll up and down.
*/
( function( $ ) {
var lastScrollTop = 0, delta = 20;
$(window).scroll(function(){
@MichaelVanDenBerg
MichaelVanDenBerg / genius_add_search_toggle.php
Created February 3, 2016 15:36
Add a search toggle after the regular menu items.
function genius_add_search_toggle ( $items, $args ) {
if ( $args->theme_location == 'social') {
$items .= '<li id="search-toggle" class="menu-item"><a href="#"><span class="screen-reader-text">Search Toggle</span></a></li>';
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'genius_add_search_toggle', 10, 2 );
/**
* Turn a multi-dimensional array in to a simple flat array.
*
* @link http://stackoverflow.com/a/14972714/4429450
*/
function array_flatten($array) {
$return = array();
foreach ($array as $key => $value) {
if (is_array($value)){ $return = array_merge($return, array_flatten($value));}
else {$return[$key] = $value;}
@MichaelVanDenBerg
MichaelVanDenBerg / portfolio-page-url.php
Last active April 14, 2016 19:30
Get the url of the portfolio page if the number of items to be shown is lower than the total number of items.
if ( ! function_exists( 'genius_portfolio_url' ) ) :
/**
* Echo a read more url for the portfolio page if the total posts is higher dan the displayed posts.
*/
function genius_portfolio_url() {
// Return if the post limit is more than total posts.
$post_limit = get_theme_mod( 'genius_portfolio_limit', 9 );
$total_posts = wp_count_posts( 'jetpack-portfolio' )->publish;
if ( $post_limit > $total_posts ) {
/**
* Add featured image as background image to post navigation elements.
*
* @see wp_add_inline_style()
*/
function alpha_centauri_post_nav_background() {
if ( ! is_single() ) {
return;
}
@MichaelVanDenBerg
MichaelVanDenBerg / remove-empty-p.js
Created May 28, 2016 14:41
Remove empty <p> tags with jQuery.
/**
* Remove empty <p> tags.
*
* See: http://stackoverflow.com/questions/27781798/wordpress-retain-formatting-when-calling-extended-content#comment43990361_27782619
* This seems to be the easiest solution. Remove this function if this ever gets fixed.
* Credits: http://stackoverflow.com/questions/6092855/how-do-i-remove-empty-p-tags-with-jquery
*/
( function( $ ) {
$( 'p' ).each( function() {
@MichaelVanDenBerg
MichaelVanDenBerg / big-image-paragraph-parent.js
Created July 2, 2016 18:21
Add .large class to the <p> parent of images wider than 680px. This used to break out large images in posts and pages in the Alpha Centauri WordPress theme.
// Add .large class to the <p> parent of images wider than 680px.
$( '.entry-content img' ).each( function() {
var $this = $( this );
if ( ( $this.attr( 'width' ) > 680 ) && ( $this.parent().is( 'p' ) ) ) {
$this.parent().addClass( 'large' );
}
});
@MichaelVanDenBerg
MichaelVanDenBerg / combined-menu-with-search-toggle.php
Created August 14, 2016 19:56
Displays both the primary and social navigation menu and the search toggle.
<?php
/**
* Displays both the primary and social navigation menu and the search toggle.
*/
$search = '<li id="search-toggle" class="menu-item"><a href="#"><span class="screen-reader-text">Search Toggle</span></a></li>';
$social = wp_nav_menu(
array(
'theme_location' => 'social',
@MichaelVanDenBerg
MichaelVanDenBerg / functions-content-width.php
Last active October 2, 2016 12:51
Set the content width in pixels and adjust the content width for full width pages.
<?php
/**
* Set the content width in pixels, based on the theme's design and stylesheet.
*
* Priority 0 to make it available to lower priority callbacks.
*
* @global int $content_width
*/
function bookmark_content_width() {
$GLOBALS['content_width'] = apply_filters( 'bookmark_content_width', 800 );