views:

241

answers:

1

Hi all,

I'm trying to add some functionality from a plugin I have made into a Wordpress theme but I am having little joy. The documentation doesn't really help me solve the problem so perhaps someone here can help.

I have a plugin in Wordpress that is activated and working fine. The class for this plugin has a function called generateHtml which I would like to access from a Wordpress Theme. But whatever I try, I cannot seem to access my plugin's code.

Can either give me a summary of what I need to do to get a theme accessing code from a plugin and/or point out there I am going wrong in my code:

Plugin:

<?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'));
}

Portion from theme header page:

<?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';
    }
?>

Currently all I ever see is "Nope" and "Function NOT found". Thanks for any input.

Lee,

+2  A: 

For starters, "imagerotator_show" is not a function; it's the name of a type of action. When you use the add_action() function, Wordpress just adds your method to the list of functions/methods to call when a particular action is triggered. Thus your second test will always respond with 'Function NOT found'.

The most likely cause of the first problem is failing to declare the method you want to call as a public method. You're also making the code harder than it needs to be.

The best practice I've seen for declaring methods and registering hooks from a class looks something like this:

if ( ! class_exists( 'Foo' ) ):
  class Foo {
    function __construct() {
      add_action( 'hook_name', array( &$this, 'my_hook_implementation' ) );
    }

    function my_hook_implementation() {
      // does something
    }

    public function my_special_method() {
      // does something else
    }
  }

if ( class_exists( 'Foo' ) ):
  $MyFoo = new Foo();

This allows your class to keep all of its implementation details private. When you need to call my_special_method(), you do it as follows:

$MyFoo->my_special_method();
Craig Trader
Thanks for the input. I'll give it a shot
Lee Theobald