【问题标题】:Wordpress: Accessing A Plugin's Function From A ThemeWordpress:从主题访问插件的功能
【发布时间】:2009-10-23 18:19:03
【问题描述】:

我正在尝试从我制作的插件中添加一些功能到 Wordpress 主题中,但我没有什么快乐。文档并不能真正帮助我解决问题,所以也许这里有人可以提供帮助。

我在 Wordpress 中有一个已激活且工作正常的插件。这个插件的类有一个名为 generateHtml 的函数,我想从 Wordpress 主题中访问它。但无论我尝试什么,我似乎都无法访问我的插件代码。

可以给我一个摘要,说明我需要做什么才能从插件中获取主题访问代码和/或指出我的代码有问题:

插件:

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

来自主题标题页的部分:

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

目前我所看到的只是“否”和“未找到功能”。感谢您的任何意见。

李,

【问题讨论】:

标签: plugins wordpress themes


【解决方案1】:

对于初学者来说,“imagerotator_show”不是一个函数;它是一种动作的名称。当您使用 add_action() 函数时,Wordpress 只是将您的方法添加到函数/方法列表中,以便在触发特定操作时调用。因此,您的第二个测试将始终以“未找到功能”响应。

第一个问题的最可能原因是未能将要调用的方法声明为公共方法。你也让代码变得比它需要的更难。

我见过的从类中声明方法和注册钩子的最佳实践是这样的:

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

这允许您的类将其所有实现细节保密。当你需要调用 my_special_method() 时,你可以这样做:

$MyFoo->my_special_method();

【讨论】:

  • 感谢您的意见。我试试看
  • 我很好奇你为什么在第 4 行通过引用传递$this。我是新来的 WordPress 插件使用 OO 表单。
【解决方案2】:

@andrew 因为我无法发表评论,所以我想我会回答你的辅助问题。见:

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

这里解释了当从一个对象定义一个回调函数时,你必须使用数组函数。它基本上是说从对象 $this 中获取函数 'my_hook_implementation' 并将其用作添加操作挂钩的回调参数。这是因为您在对象的范围内定义了函数,并且您必须定义范围才能让 PHP 知道您在说什么函数。作用域是变量 $this 所引用的对象。

【讨论】:

    【解决方案3】:

    您只需要在主题中使用do_action() 函数即可。

    如果您希望函数 generateHtml 出现在 header.php 中,您只需打开 header.php 文件并将 &lt;?php do_action('imagerotator_show'); ?&gt; 粘贴到您想要的位置,然后您的函数将在那里被调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-11
      • 1970-01-01
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      • 2015-12-08
      • 2017-01-16
      相关资源
      最近更新 更多