Skip to content

Instantly share code, notes, and snippets.

@gyrus
gyrus / gist:3157067
Created July 21, 2012 20:32
Get menu items of WordPress nav menu attached to specified menu location
<?php
/**
* Get menu items of nav menu attached to specified menu location
*
* @uses get_nav_menu_locations()
*/
function pilau_get_menu_location_items( $location_slug, $max = 0 ) {
static $menu_locations = null;
if ( $menu_locations === null )
@gyrus
gyrus / gist:3157070
Created July 21, 2012 20:34
Get WordPress nav menu without markup containers
<?php
/**
* Get nav menu without markup containers
*
* @uses wp_nav_menu()
* @param string $menu The name given to the menu in Appearance > Menus
* @param integer $depth
* @return string
*/
@gyrus
gyrus / gist:3157158
Created July 21, 2012 20:54
Output WordPress content without first paragraph
<?php
/**
* Output WordPress content without first paragraph
*
* @uses get_the_content()
*/
function pilau_the_content_without_first_paragraph() {
$content = trim( apply_filters( 'the_content', get_the_content() ) );
$content = preg_replace( '#<[a-z]+>[^<]+</[a-z]+>#', '', $content, 1 );
@gyrus
gyrus / gist:3157161
Created July 21, 2012 20:55
Output first paragraph of WordPress content
<?php
/**
* Output first paragraph of WordPress content
*
* @uses get_the_content()
*/
function pilau_the_content_first_paragraph() {
$content = trim( apply_filters( 'the_content', get_the_content() ) );
preg_match( '#<[a-z]+>[^<]+</[a-z]+>#', $content, $matches );
@gyrus
gyrus / gist:3157179
Created July 21, 2012 21:04
Simple list of download files attached to WordPress post or page
<?php
/**
* List of downloads
*
* @uses get_children()
* @uses esc_url()
* @uses apply_filters()
* @uses get_attached_file()
* @uses pilau_format_filesize() (find on Gist)
@gyrus
gyrus / gist:3157188
Created July 21, 2012 21:05
Format the size of a file (for WordPress)
<?php
/**
* Return the formatted size of a file.
*
* @param string|int $input Either the path to a valid file, or a number in bytes
* @param string $default_output Optional. The string to output if the input can't be used (e.g. the file doesn't exist)
* @uses size_format()
* @return string The size, formatted
*/
@gyrus
gyrus / gist:3157198
Created July 21, 2012 21:09
Check if a WordPress page is "current"
<?php
/**
* Check if a WordPress page is "current", i.e. it's the current page or the ancestor of a current page
*
* @uses is_search()
* @uses get_post_type()
* @uses site_url()
* @uses get_permalink()
* @uses get_post_type_object()
@gyrus
gyrus / gist:3157207
Created July 21, 2012 21:15
Fix height of Flickr slideshows embedded in WordPress
<?php
/**
* Fix height of embedded Flickr slideshows
*/
add_filter( 'oembed_result', 'pilau_fix_flickr_slideshow_height', 10, 3 );
function pilau_fix_flickr_slideshow_height( $html, $url, $args ) {
if ( strpos( $url, "flickr.com" ) !== false && substr( $url, -6 ) == "/show/" )
$html = preg_replace( '/ height="([^"]+)"/', ' height="350"', $html );
return $html;
@gyrus
gyrus / preload-images.js
Last active March 3, 2021 11:57
WordPress image preloading
/**
* Image preloader
*
* @link http://engineeredweb.com/blog/09/12/preloading-images-jquery-and-javascript
*/
var cache = [];
// Arguments are image paths relative to the current page.
function pilau_preload_images() {
var args_len, i, cache_image;
args_len = arguments.length;
@gyrus
gyrus / gist:3157247
Created July 21, 2012 21:28
Scroll to an ID
/**
* Scroll to an ID
*
* Example:
* <code>$( '#content' ).scrollView();</code>
*
* @link http://stackoverflow.com/questions/1586341/how-can-i-scroll-to-a-specific-location-on-the-page-using-jquery
*/
jQuery.fn.scrollView = function () {
return this.each( function () {