WordPress:从主题访问插件的功能

发布于 2024-08-08 13:36:22 字数 1598 浏览 3 评论 0原文

我正在尝试从我制作的 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';
    }
?>

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

李,

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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷爱 2024-08-15 13:36:22

对于初学者来说,“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();

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();
你爱我像她 2024-08-15 13:36:22

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

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

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

@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.

猫弦 2024-08-15 13:36:22

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

如果您希望函数 generateHtml 出现在 header.php 中,您只需打开 header.php 文件并粘贴 你想要的地方,然后你的函数将在那里被调用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文