Either should work, are you experiencing any errors?
Thread Starter
leesy
(@leesy)
No, I can’t see any. I think I am just missing on final piece of code. My plugin looks a little like this:
<?php
/** Usual comments here **/
if (!class_exists("ImageRotator")) {
class ImageRotator {
private $uploadPath = '';
private $pluginPath = '';
private $options;
function __construct() {
$this->uploadPath = dirname(__file__).'\\uploads\\';
// add_shortcode('imagerotator', array(&$this, 'generateHtml'));
}
// Various functions for plugin
function generateHtml() {
echo '<p>Hello World</p>';
}
}
}
/**
* Create instance of image rotator
*/
$imageRotator = new ImageRotator();
/**
* Create actions & filters for WordPress
*/
if (isset($imageRotator)) {
// Actions
add_action('admin_menu', array(&$imageRotator, 'createMenu'));
add_action('admin_init', array(&$imageRotator, 'registerSettings'));
add_action('imagerotator_show', array(&$imageRotator, 'generateHtml'));
}
And in my header all I have added is:
<?php if (isset($imageRotator)) {
$imageRotator->generateHtml();
} else if (isset($ImageRotator)) {
print_r($ImageRotator);
} else {
echo '<p>Nope!</p>';
}
if (function_exists("imagerotator_show")) {
echo 'Function found';
} else {
echo 'Function NOT found';
}
?>
Which also displays “Nope” and “Function not found”.
So what am I missing? Do I need to include my plugin again somewhere as I was assuming WordPress would handle this for me.
In the header, you will want to declare $imageRotator
as global. header.php is included via the get_header() function meaning that it’s scope is dictated by that function. I would use this block:
if ( isset( $imageRotator ) )
$imageRotator->generateHtml();
else
echo '<p>Nope!</p>';
FYI – You would never run into this problem using actions and filters.