Skip to content

Instantly share code, notes, and snippets.

@jeweltheme
jeweltheme / gist:0b60a01c09c0fbc9318fb776b7c1a2ed
Created May 13, 2017 19:51 — forked from mattboon/gist:1275191
WordPress - Query Custom Post Type and taxonomy term by ID
<?php
// gets the ID from a custom field to show posts on a specific page
$buildType = get_post_meta($post->ID, 'build_type_id', true);
// run query
query_posts(array(
'post_type' => 'portfolio',
'showposts' => -1,
'tax_query' => array(
array(
@jeweltheme
jeweltheme / Video generator
Created May 15, 2017 20:31 — forked from jimmylatreille/Video generator
Youtube, Vimeo and Dailymotion script
<?php
if(preg_match('/vimeo/', $data['url'])){
$url = '//player.vimeo.com/video/'.end(explode('/', $data['url']));
echo "<iframe src='$url' width='560' height='315' frameborder='0' webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>";
echo "<img src='".unserialize(file_get_contents("http://vimeo.com/api/v2/video/".end(explode('/', $data['url'])).".php"))[0]['thumbnail_medium']."' />";
@jeweltheme
jeweltheme / gist:72c51fa5639bd29dd3570e68ffe1f876
Created May 17, 2017 04:18 — forked from wwdboer/gist:4353522
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' ) );
@jeweltheme
jeweltheme / gist:1106d5845db0e81a43c32e57ebd2d1ab
Created May 17, 2017 04:29 — forked from wwdboer/gist:4730303
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
/**
* 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;
}
}
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
@jeweltheme
jeweltheme / gist:fb80063d7aa8e59c99f1cc27890fc10a
Created May 17, 2017 04:33 — forked from wwdboer/gist:4353758
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];
}
?>
@jeweltheme
jeweltheme / gist:1aa535d1d211be91e223eb909ad96699
Created May 17, 2017 04:34 — forked from wwdboer/gist:4353695
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/' );
@jeweltheme
jeweltheme / remote-image-cache.php
Created May 18, 2017 21:08 — forked from jasondavis/remote-image-cache.php
Cache remote image using PHP
<?php
function cache_image($image_url){
//replace with your cache directory
$image_path = 'path/to/cache/dir/';
//get the name of the file
$exploded_image_url = explode("/",$image_url);
$image_filename = end($exploded_image_url);
$exploded_image_filename = explode(".",$image_filename);
$extension = end($exploded_image_filename);
//make sure its an image
@jeweltheme
jeweltheme / custom-post-taxonomy-permalinks.php
Created May 28, 2017 18:13 — forked from kasparsd/custom-post-taxonomy-permalinks.php
Create permalink structure URLs for custom post types that include all parent terms from a custom taxonomy
<?php
/*
Term Archive Pages:
- http://example.com/recipes/dinner/
- http://example.com/recipes/breakfast,brunch/
Single Recipe Pages:
- http://example.com/recipes/dinner/soup-title/