WordPress:从主题访问插件的功能 [英] Wordpress: Accessing A Plugin's Function From A Theme
问题描述
我正在尝试从我制作成Wordpress主题的插件中添加一些功能,但我对此并不高兴.该文档并不能真正帮助我解决问题,因此也许有人可以提供帮助.
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.
我在Wordpress中有一个已激活且工作正常的插件.该插件的类具有一个称为generateHtml的函数,我想从Wordpress主题访问该函数.但是无论我如何尝试,我似乎都无法访问我插件的代码.
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:
插件:
<?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';
}
?>
目前我所看到的只是没有"和找不到功能".感谢您的输入.
Currently all I ever see is "Nope" and "Function NOT found". Thanks for any input.
李
推荐答案
对于初学者而言,"imagerotator_show"不是函数;这是一种动作的名称.当您使用add_action()函数时,Wordpress只会将您的方法添加到触发特定操作时要调用的函数/方法列表中.因此,您的第二项测试将始终以找不到功能"为响应.
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();
这使您的班级可以将所有实现细节保密.当您需要调用my_special_method()时,请按以下步骤进行操作:
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();
这篇关于WordPress:从主题访问插件的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!