Skip to content

Instantly share code, notes, and snippets.

View gregpalaci's full-sized avatar

Greg Palaci gregpalaci

  • Cactus & Dove Ltd.
  • London, UK
View GitHub Profile
@gregpalaci
gregpalaci / closestNumber
Created May 9, 2013 10:34
Pass an array and a value, get back the closest number.
var getClosestNum = function(num, ar) {
var i = 0, closestDiff, currentDiff;
if(ar.length) {
closest = ar[0];
for(i;i<ar.length;i++) {
closestDiff = Math.abs(num - closest);
currentDiff = Math.abs(num - ar[i]);
if(currentDiff < closestDiff) {
closest = ar[i];
}
@gregpalaci
gregpalaci / stringToJSON
Created May 26, 2013 10:16
Better way to convert string to JSON
try{
return JSON.parse(my_string);
}catch(err){
return JSON.parse('"'+my_string+'"');
}
@gregpalaci
gregpalaci / jquery.promise
Created May 26, 2013 10:19
Using Jquery Promise for callback after animations
var items = $('.element');
items.animate(
{opacity:0},
{
duration:2000,
complete: function(){
// This would be called on each item
}
}
@gregpalaci
gregpalaci / wp_removeImageHW
Created May 26, 2013 11:17
WordPress: remove height and width from image tag
add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );
function remove_width_attribute( $html ) {
$html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
return $html;
}
@gregpalaci
gregpalaci / WP.AllowSVG
Created May 26, 2013 11:18
WordPress: Allow SVG with uploader
function cc_mime_types( $mimes ){
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
add_filter( 'upload_mimes', 'cc_mime_types' );
@gregpalaci
gregpalaci / wp.jqueryCDN
Created May 26, 2013 11:20
WordPress: Replace jquery with CDN version
if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
function my_jquery_enqueue() {
wp_deregister_script('jquery');
wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", false, null);
wp_enqueue_script('jquery');
}
@gregpalaci
gregpalaci / WP.shortcodeTemplateFile
Created May 26, 2013 11:22
WordPress: Do a shortcode in template file
<?php echo do_shortcode("[shortcode]"); ?>
@gregpalaci
gregpalaci / WP.cssCacheBust
Created May 26, 2013 11:26
WordPress: quick cache bust for stylesheets
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); echo '?' . filemtime( get_stylesheet_directory() . '/style.css'); ?>" type="text/css" media="screen">
@gregpalaci
gregpalaci / WP.getFeaturedImage
Created May 26, 2013 13:01
WordPress: Get featured image url
<?php
$imgsrc = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "Full");
echo $imgsrc[0];
?>
@gregpalaci
gregpalaci / WP.limitWords
Created May 26, 2013 13:03
WordPress: Limit words function
<?php
function limit_words($string, $word_limit) {
$words = explode(' ', $string);
return implode(' ', array_slice($words, 0, $word_limit));
}
?>
#usage:
<?php echo limit_words(get_the_excerpt(), '41'); ?>