views:

42

answers:

1

Hi All

I am trying to learn more about Wordpress and creating plugins. I have seen an existing plugin use a technique where you can add a 'reference' to it within your posts and WP will parse it and replace it with the plugins own content. The example i am referring to is the NextGen gallery which uses the following code

[nextgen id=9]

I have tried searching for how this technique works but trying to find something that you dont know the name of is pretty difficult!

Can anyone point me towards some resources about how to use this feature of WP?

Thanks in advance Stuart

+2  A: 

The technique is called shortcodes.

add_shortcode('my-content','my_plugin_shortcode');
function my_plugin_shortcode($atts, $content = null) {
  $atts = shortcode_atts($my_default_atts,$atts);  // $atts is now an associate array
  $my_content = 'This is some content.';
  return $my_content;
}

Then, if you have a post with the following content:

Hey, here is some content. [my-content]

You will get the following output when the post is displayed:

Hey, here is some content. This is some content.

If you passed a shortcode like [my-content id="9" test="test"], then the $atts variable in the above function would be like the following array declaration

$atts = array('id'=>9, 'test'=>'test');

The $content variable only has content when you use matching shortcodes around some text:

[my-content]This is some test content.[my-content]

nickohrn
Thanks very much Nickohrn! thats exactly what i was after! Cheers, Stuart