This spun out of someone who was _doing_it_wrong(). His plugin embed code reinvented all four wheels. First he was doing a reg4exp to implement shortcodes, then he was replicating oEmbeds that WordPress already has. I told him to first remove the ones that existed (so as not to cause conflicts) and then look up shortcodes.
But since the shortcodes he wanted to make were a little weird, I thought “Well, I know I would like to see the CBS Videos one, since I embed a lot of them…” So I took ten minutes to eat a cracker and re-write his code.
Here’s my code first.
// CBS Video Shortcode function cbsvideo_func( $atts ) { extract( shortcode_atts( array( 'id' => '00', 'width' => '480', 'height' => '270', ), $attr ) ); return '<object width="'.$width.'" height="'.$height.'"><param name="movie" value="http://www.cbs.com/e/'.$id.'/cbs/1/" /></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed width="'.$width.'" height="'.$height.'" src="http://www.cbs.com/e/'.$id.'/cbs/1/" allowFullScreen="true" allowScriptAccess="always" type="application/x-shockwave-flash"></embed></object>'; } add_shortcode( 'cbsvideo', 'cbsvideo_func' );
Then you use [cbsvideo id="9wFRf8KYoAXipnXgA8GKIm1VNaMgKjpE"]
to embed it and have a beer. I put it in a file called sitewide-functions.php in my mu-plugins folder and walked away.
Here’s his code, as a reference.
define("CBS_WIDTH", 480); // default width define("CBS_HEIGHT", 270); // default height define("CBS_REGEXP", "/\[cbs ([[:print:]]+)\]/"); define("CBS_TARGET", "<object width=\"###WIDTH###\" height=\"###HEIGHT###\"><param name=\"movie\" value=\"http://www.cbs.com/e/###URL###/cbs/1/\" /></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowScriptAccess\" value=\"always\"></param><embed width=\"###WIDTH###\" height=\"###HEIGHT###\" src=\"http://www.cbs.com/e/###URL###/cbs/1/\" allowFullScreen=\"true\" allowScriptAccess=\"always\" type=\"application/x-shockwave-flash\"></embed></object>"); function cbs_plugin_callback($match) { $tag_parts = explode(" ", rtrim($match[0], "]")); $output = CBS_TARGET; $output = str_replace("###URL###", $tag_parts[1], $output); if (count($tag_parts) > 2) { if ($tag_parts[2] == 0) { $output = str_replace("###WIDTH###", CBS_WIDTH, $output); } else { $output = str_replace("###WIDTH###", $tag_parts[2], $output); } if ($tag_parts[3] == 0) { $output = str_replace("###HEIGHT###", CBS_HEIGHT, $output); } else { $output = str_replace("###HEIGHT###", $tag_parts[3], $output); } } else { $output = str_replace("###WIDTH###", CBS_WIDTH, $output); $output = str_replace("###HEIGHT###", CBS_HEIGHT, $output); } return ($output); } function cbs_plugin($content) { return (preg_replace_callback(CBS_REGEXP, 'cbs_plugin_callback', $content)); } add_filter('the_content', 'cbs_plugin',1); add_filter('the_content_rss', 'cbs_plugin'); add_filter('comment_text', 'cbs_plugin');
35 lines of code vs 11, and my 11 will run faster.
Comments
3 responses to “Embedding CBS Video”
Why make a whole shortcode for it when you can use the built in oembed system? Yeah, okay, CBS doesn’t have oembed… doesn’t mean you can’t still use the WP embeds. π
http://pastebin.com/NKUnJu2r
The resulting shortcode for this would be to put the URL of the video from your browser into an embed shortcode, same as all others. Width and height can be specified as attributes. And if you have enabled auto-link-embedding, then this will recognize the URL on a line by itself like all the other embeds.
Adding onto the embed system is easy with wp_embed_register_handler. π
Whoops, I forget to use the width and height in the object. New paste:
http://pastebin.com/H479YMKF
Okay, seriously cool! Making notes for next times! π