Last active
July 30, 2020 16:55
-
-
Save nicholasohrn/9491724 to your computer and use it in GitHub Desktop.
Automatically embed Dribbble shots into WordPress content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is super rough and something I threw together as a proof of concept more than anything. If you're interested, please let me know and I can turn this into something a little more robust :)