Mar 11

Recently used a shortcode provided by a plugin and found the content was not being inserted where I had placed the shortcode. Instead the shortcode was inserting the content at the top of my page.

Having a look at the source for the plugin uncovered the following.

// Shortcode implementation
function magic_stuff($atts) {
	include(TEMPLATEPATH.'/wp-content/prugins/magic-plugin/magic-code.php');
}
//register the Shortcode handler
add_shortcode('magic', 'magic_stuff');

The problem here is that the shortcode handler needs to return the string that needs to be inserted into the post or page. Any string returned by the shortcode handler will be inserted into the post body in place of the shortcode itself.

In our case the string was not being returned and so appeared at the top of our post.

To fix this we can use the php output buffers with ob_start() and ob_get_clean() functions to assign the content of the include to a variable as shown in the example below.

// Shortcode implementation
function magic_stuff($atts) {

   // turn on output buffering to capture script output
	
   ob_start();

   // include file (contents will get saved in output buffer)
	
   include(TEMPLATEPATH.'/wp-content/prugins/magic-plugin/magic-code.php');

   // save and return the content that has been output

   $content = ob_get_clean();
   return $content;
}
//register the Shortcode handler
add_shortcode('magic', 'magic_stuff');

This is also a great way to easily include php code inside your posts and pages. Just create a shortcode that includes your required snippet. You can then easily add that shortcode whereever you need it.

written by Obiweb \\ tags: , ,

Dec 22

After upgrading one our clients WordPress installations to 2.9 we started getting the following notice:

Notice: get_settings is deprecated since version 0.0! Use get_option() instead. in /var/www/wp/wp-includes/functions.php on line 2970

After a bit of digging it was found that the audio-player plugin was the culprit. To fix it just required replacing calls to ‘get_settings’ with ‘get_option’.

written by Obiweb \\ tags: , , ,