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: , ,

Sep 06

Leopard (10.5) was the first release of MacOS that included PHP 5 support. However by default it is disabled.
To enable it you need to uncomment (remove the #) from the following line in /private/etc/apache2/httpd.conf.

#LoadModule php5_module libexec/apache2/libphp5.so

Then restart apache

sudo /usr/sbin/apachectl restart

written by objects \\ tags: , ,