Skip to content

Instantly share code, notes, and snippets.

@nicholasohrn
Last active July 30, 2020 16:55
Show Gist options
  • Save nicholasohrn/9491724 to your computer and use it in GitHub Desktop.
Save nicholasohrn/9491724 to your computer and use it in GitHub Desktop.
Automatically embed Dribbble shots into WordPress content
<?php
/*
Plugin Name: Dribbble Shot Embed
Description: Automatically embed a shot into your WordPress site just by dropping the URL in place.
Version: 1.0.0.RC.1
Author: Nick Ohrn of Plugin-Developer.com
Author URI: http://plugin-developer.com/
*/
function dribbble_shot_embed_callback($matches, $attr, $url, $rawattr) {
$output = false;
if(($id = isset($matches[1]) ? $matches[1] : false)) {
$key = sprintf('dribbble_shot_%d', $id);
$shot = get_transient($key);
if(false === $shot) {
$response = wp_remote_get(sprintf('http://api.dribbble.com/shots/%d', $id));
if(!is_wp_error($response)) {
$shot = json_decode(wp_remote_retrieve_body($response), true);
if($shot) {
set_transient($key, $shot, DAY_IN_SECONDS);
}
}
}
if($shot) {
$title = esc_attr($shot['title']);
$url = esc_attr(esc_url($shot['url']));
$image_height = absint($shot['height']);
$image_src = esc_attr(esc_url($shot['image_url']));
$image_width = absint($shot['width']);
$output = sprintf('<a href="%1$s" target="_blank" title="%2$s"><img alt="%2$s" src="%3$s" height="%4$d" width="%5$d" /></a>', $url, $title, $image_src, $image_height, $image_width);
}
}
return $output;
}
function dribbble_shot_embed_wp_embed_register_handler() {
wp_embed_register_handler('dribbble-shot-embed', '#^http://dribbble.com/shots/(\d+)#i', 'dribbble_shot_embed_callback');
}
add_action('init', 'dribbble_shot_embed_wp_embed_register_handler');
@RomanSima
Copy link

Doesn't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment