WordPress Short Code Plugin

It wasn’t too long ago I wrote my first WordPress plugin. Essentially, I Googled “wordpress plugin tutorial” and wandered from there. Another helpful piece for me is downloading plugins that do something close to what I want and trying to figure out what they did.

Inspired in part by Boone Gorges and his description of how he handled iframe embed issues in multisite, and David asking for some way for students to embed Debate Graph I figured I could write a plugin to allow a shortcode embed for that site.

The dead simple example is below.1 It allows you to put [debategraph url=”http://debategraph.com/whatevertheurl”] and it returns the needed embed code. I’ll probably set it up with height/width as a user option but I wanted it as pure as possible initially. I’ll also need to tie them in with the main WP editor sooner or later.

In writing this and trying to piece together how I figured this out, I also found this shortcode generator which might be very helpful in the future. This was hiding2 at the bottom of the WP Codex page on shortcodes.

<?php

// [debategraph url="url"]
function debategraph_shortcode( $atts ) {
	extract ( shortcode_atts ( array(
		'url' => 'debategraph page url'
		), $atts ) );
	return'<iframe src="' . $url . '" frameborder="0" width="700px" height="550px" scrolling="auto" </iframe>';
}
add_shortcode( 'debategraph', 'debategraph_shortcode' );
?>

This is pretty much a barebones template so when we had a little stutter this morning around embedding Google Forms. I was able to alter it in a few minutes and have something functional. I’ll paste it below in case it helps you understand a bit more by contrasting the two.

<?php
// [google url="url"]
function google_shortcode( $atts ) {
	extract ( shortcode_atts ( array(
		'url' => 'google url'
		), $atts ) );
	return'<iframe src="' . $url . '" frameborder="0" width="700px" height="800px" scrolling="auto" </iframe>';
}
add_shortcode( 'google', 'google_shortcode' );
?>

1 I cut out the plugin comment stuff for clarity but you’ll need it.

2 or obviously located