Wordpress: Accessing A Plugin's Function From

2020-02-11 03:34发布

问题:

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,

回答1:

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();


回答2:

@andrew since I can't comment I thought I would answer your ancillary question. See:

http://net.tutsplus.com/tutorials/wordpress/create-wordpress-plugins-with-oop-techniques/

Where it is explained that when defining a callback function from an object you have to use the array function. It's basically saying get the function 'my_hook_implementation' from the object $this and use it as the callback parameter to the add action hook. It is because you defined the function within the scope of the object and you have to define the scope in order for PHP to know what function you are talking about. The scope being the object referred to by the variable $this.



回答3:

You just need to use do_action() function, inside your theme.

If you want the function generateHtml to appears inside your header.php you just need to open the header.php file and paste <?php do_action('imagerotator_show'); ?> where you want and then your function will be called there.