Support » Plugins » Accessing My Plugin From A Theme

  • Hi all,

    I’m sure this must be answered somewhere but I had a good search around and couldn’t find anything. I’ve created a plugin that a user can edit etc. Now I want to be able to add this in my theme’s PHP files using something like below:

    if (isset($imageRotator)) {
      $imageRotator->generateHtml();
    }

    or

    if (function_exists("imageRotatorShow")) {
      imageRotatorShow();
    }

    And I’ve been looking on how to do it but I can’t find anything telling me how. As far as as I can tell, I don’t want to be using an action or a filter.

    Can someone point me in the way of an example of how I can access my plugin from a theme please.

    Cheers,
    Lee

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Accessing My Plugin From A Theme’ is closed to new replies.