Wordpress:PHP 致命错误:调用未定义的函数 get_option() [英] Wordpress: PHP Fatal error: Call to undefined function get_option()

查看:23
本文介绍了Wordpress:PHP 致命错误:调用未定义的函数 get_option()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎搜索了所有地方,但建议的答案对我没有帮助.

I've searched almost everywhere, but proposed answers didn't help me.

问题:我安装了 Wordpress,最新版本 (3.6.1).我已经进行了多次全新安装,查看了 wp-includes/option.php 和其他文件,我很确定它们都可以正常工作并且都具有正确的内容.

Problem: I've got a Wordpress installation, last version (3.6.1). I've done a clean install multiple times, looked into the wp-includes/option.php and other files and I'm pretty sure it all works and all has the correct content.

我正在开发一个插件,我正在使用 Wordpress 定义的函数 get_option.每当我的代码调用该函数时,我都会收到 500: internal server error 响应.奇怪,因为插件的代码应该从Wordpress框架内调用...

I'm developing a plugin, and I'm making use of the Wordpress-defined function get_option. Whenever my code calls that function, I get a 500: internal server error response. Weird, cause the code of a plugin should be called from within the Wordpress framework...

让它变得更奇怪:在这些包含文件中定义的其他函数,比如 add_options_page,可以完美地工作并且表现得像他们应该的那样.

Make it even more weird: other functions defined in those included files, like add_options_page, work perfectly and behave like they should.

因此,例如,这是有效的:

So, for example, this works:

$pageTitle = "Title for my Options Page";
$menuLink = "Title for my Menu Link";
$userAccessLevel = 8; //that's admin
$pageSlug = "slug-to-my-plugin-options-page";
$callbackFunction = array($this, 'optionsPage');
add_options_page($pageTitle, $menuLink, $userAccessLevel, 
        $pageSlug, $callbackFunction);

但这不会:

get_option("ntp_myoption");

add_options_pageget_option 都定义在同一文件夹中的源文件中(wp-includes\option.phpwp-includes\plugin.php),这两个函数都在这些文件中有效,上面的两个代码块都在我的插件中的同一个文件中,我没有包含或需要任何文件.

Both add_options_page and get_option are defined in source files in the same folder (wp-includes\option.php and wp-includes\plugin.php), both functions are effectively in those files, both blocks of code above are in the same file in my plugin, I didn't include or require any file.

有人知道吗?

如所问,我调用 get_option 的完整代码块 - 它来自包装插件的类的构造函数.

As asked, the full block of code from where I call get_option - it is from the constructor of my class that wraps the plugin.

function __construct() {
    global $wpdb;
    $this->table_iso = $wpdb->prefix . "ntp_iso";
    $this->pluginUrl = get_option('siteurl') . '/wp-content/plugins/my-plugin';
}

也许还值得一提:我有一个包装实际插件的类,在那个 .php 文件的底部,我有(在类定义之外),这段代码:

Also maybe worth to mention: I've got a class that wraps the actual plugin, and in the bottom of that .php file, I've got (outside the class definition), this code:

global $tp;
$tp = new MyPlugin();
$plugin = plugin_basename(__FILE__);

register_activation_hook( __FILE__, array($tp, 'install'));
register_deactivation_hook( __FILE__, array($tp, 'deactivate'));
add_action('add_meta_boxes', array($tp, 'init'));
if (is_admin()) {
    add_action('admin_menu', array($tp, 'addOptionsPage'));
    add_filter("plugin_action_links_$plugin", array($tp, 'addSettingsLink'));
}

这些都像魅力一样.

推荐答案

我怀疑 wp-includes\option.php 没有被加载.

I suspect wp-includes\option.php is not being loaded.

在调用 get_options() add 之前只是为了咧嘴笑

Just for grins, right before the call to get_options() add

include_once('wp-includes\option.php');

或者尝试在 option.php 中调用其他内容,例如:update_option(null);

Or try calling something else in option.php like: update_option(null);

option.php 包含在 wp-includes/functions.php 中,而 plugin.php 包含在几个不同的地方之一.

option.php is included from inside wp-includes/functions.php while plugin.php gets included in one of several different places.

您可以通过将其插入代码中来查看当前包含/需要的所有文件:

You can see all the files that are currently included/required by inserting this into your code:

 $includedStuff = get_included_files();
 print_r($includedStuff);

祝你好运!

这篇关于Wordpress:PHP 致命错误:调用未定义的函数 get_option()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆