Skip to content

Instantly share code, notes, and snippets.

@mykebates
Created February 1, 2013 20:08
Show Gist options
  • Save mykebates/4693759 to your computer and use it in GitHub Desktop.
Save mykebates/4693759 to your computer and use it in GitHub Desktop.
// Pass in a post id and optionally a thumbnail string name and receive the image path
function getImgUrl($size, $id){
$src;
if(!$id){
$id = $post->ID;
}
if($size){
$src = wp_get_attachment_image_src( get_post_thumbnail_id($id), array( 150,150 ), false, '' );
}
else{
$src = wp_get_attachment_image_src( get_post_thumbnail_id($id),'', false, '' );
}
return $src[0];
}
// pass in a youtube video url and receive the video id. very useful for fine control over an embed
function youtubeid($url) {
if (preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $url, $match)) {
$match = $match[0];
}
return $match;
}
// pass in a string with roughly the amount of characters you would like to limit to, optionally an ending string - default is hellip. Receive a string that is roughly limited to amount of characters but the last word will not be trimmed. Not always perfect for specific lengths but otherwise pretty nice and much nicer than chopped off words.
function excerpt_char($text, $lim, $delim="&hellip;")
{
$len = strlen($text);
if ($len <= $lim) return $text;
// split at first word boundary after $lim chars
preg_match('/(.{' . $lim . '}.*?)\b/', $text, $matches);
$text = preg_replace("'(&[a-zA-Z0-9#]+)$'", '$1;', $matches[1]);
$text .= $delim;
return $text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment