包含wordpress插件的函数xy()

问题描述:

我想要构建一个WordPress管理仪表板小部件,它应该从另一个插件返回一些信息。包含wordpress插件的函数xy()

本面板控件应该阅读这个插件在这里的功能: http://plugins.svn.wordpress.org/wp-document-revisions/trunk/wp-document-revisions.php

所以我的代码是:

include (WP_PLUGIN_URL.'/wp-document-revisions/wp-document-revisions.php'); 
$file_type = get_file_type('3'); 

但是,这是行不通的。这些都是错误的:

Fatal error: Call to undefined function add_action() in /.../wp-content/plugins/wp-document-revisions/wp-document-revisions.php on line 26

Fatal error: Call to undefined function get_file_type() in /.../wp-content/plugins/dashboard-widget/dashboard-widget.php

谁能告诉我,我怎么也得做到这一点,我可以读取功能get_file_type(“3”)?

我假设你直接浏览wp-content/plugins /文件夹中的PHP文件,而不是使用存根创建URL。如果是这种情况,那么你可能忘记了包含wp-load.php

+0

add_action()是一个WordPress的核心功能,所以如果该功能不存在,这意味着你还没有实际加载WordPress。你可以做'include('../../../ wp-load.php');'(或者指向你的wp-load.php文件的任何点),然后你就可以使用任何活动插件。 – jprofitt

你不应该包含插件文件(因为插件可能没有安装或激活)。 取而代之的是,你应该检查是否函数存在:

if (function_exists('get_file_type')) { 
$file_type = get_file_type('3'); 
// rest of the code of the widget 
} 

这听起来像你试图直接访问PHP文件,而不是通过WordPress的正确去。你应该创建一个插件,并钩入Dashboard Widgets API

至于使用WP Document Revisions的实现,您有两种选择。从版本1.2开始,get_documents()get_document_revisions()是两个全局函数,它们将在plugins_loaded钩子后随时访问。 FAQ有更多的信息,但它们基本上像本地功能get_posts()一样运作。

或者,该类应该在全球范围内可用,如$wpdr。所以,如果你想要,例如,拨打get_latest_version(1)它会是$wpdb->get_latest_version(1)

两者都假定插件已被激活。如果你只是简单地包含这个文件,你会得到一个错误,你试图重新声明WP_Document_Revisions类。

如果您确实创建了仪表板,我很乐意将它包含在插件的未来版本中!