Skip to content

Instantly share code, notes, and snippets.

@wwdboer
wwdboer / 0_reuse_code.js
Created August 5, 2016 15:13
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@wwdboer
wwdboer / WordPress Bootstrap responsive embed
Created January 15, 2015 10:32
WordPress Bootstrap responsive embed
/**
* Wrap embed html with bootstrap responsive embed div
*/
function bootstrap_embed( $html, $url, $attr ) {
if ( ! is_admin() ) {
return "<div class=\"embed-responsive embed-responsive-16by9\">" . $html . "</div>";
} else {
return $html;
}
}
@wwdboer
wwdboer / gist:4943672
Created February 13, 2013 10:27
PHP: Get Vimeo ID
function get_vimeoid( $url ) {
$regex = '~
# Match Vimeo link and embed code
(?:<iframe [^>]*src=")? # If iframe match up to first quote of src
(?: # Group vimeo url
https?:\/\/ # Either http or https
(?:[\w]+\.)* # Optional subdomains
vimeo\.com # Match vimeo.com
(?:[\/\w]*\/videos?)? # Optional video sub directory this handles groups links also
\/ # Slash before Id
@wwdboer
wwdboer / gist:4730303
Created February 7, 2013 11:01
WordPress: Get image attachments
function get_image_attachments( $size = 'thumbail', $ID ) {
$featured_image_id = get_post_thumbnail_id( $ID );
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'post_parent' => $ID,
'exclude' => $featured_image_id
@wwdboer
wwdboer / gist:4495665
Last active December 10, 2015 21:29
JavaScript: Repeat string
String.prototype.repeat = function (num) {
return new Array(num + 1).join(this);
};
@wwdboer
wwdboer / gist:4353758
Created December 21, 2012 16:14
PHP: Get YouTube ID from URL
<?php
function get_ytid( $url ) {
preg_match( "#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $url, $matches );
return $matches[0];
}
?>
@wwdboer
wwdboer / gist:4353695
Last active May 17, 2017 04:34
WordPress: Template redirect
<?php
// In functions.php
add_action( 'template_redirect', 'redirect_template' );
function redirect_template() {
global $post;
if ( is_single() && is_post_type( 'custompost' ) ) {
wp_redirect( home_url() . '/custompost/' );
@wwdboer
wwdboer / gist:4353601
Last active December 10, 2015 00:48
WordPress: Modulo example to create rows
<?php
global $post;
$args = array(
'post_type' => 'post',
'posts_per_page' => -1
);
$new_query = new WP_Query($args);
$post_count = 0;
$total_posts = $new_query->post_count;
?>
@wwdboer
wwdboer / gist:4353522
Last active May 17, 2017 04:18
WordPress: Twitter time ago
<?php
// Twitter date format example: "created_at": "Mon Jun 27 19:32:19 +0000 2011"
function from_apachedate( $date ) {
list( $D, $M, $d, $h, $m, $s, $y ) = sscanf( $date, "%s %s %2d %2d:%2d:%2d +0000 %4d" );
return strtotime( "$d $M $y $h:$m:$s" );
}
// human_time_diff is a WordPress function
echo human_time_diff( from_apachedate( $item->created_at ), current_time( 'timestamp' ) );