Wordpress:PHP 致命错误:调用未定义的函数 get_option()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19326761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wordpress: PHP Fatal error: Call to undefined function get_option()
提问by testuser
I've searched almost everywhere, but proposed answers didn't help me.
我几乎到处搜索,但提出的答案对我没有帮助。
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,最新版本 (3.6.1)。我已经进行了多次全新安装,查看了wp-includes/option.php
和其他文件,我很确定一切正常,并且都有正确的内容。
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...
我正在开发一个插件,我正在使用 Wordpress 定义的函数get_option
。每当我的代码调用该函数时,我都会收到500: internal server error
响应。奇怪,因为插件的代码应该从 Wordpress 框架内调用......
Make it even more weird: other functions defined in those included files, like add_options_page
, work perfectly and behave like they should.
让它变得更奇怪:在这些包含文件中定义的其他函数,比如add_options_page
,完美地工作并且表现得像他们应该的那样。
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);
But this doesn't:
但这不会:
get_option("ntp_myoption");
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.
双方add_options_page
并get_option
在同一文件夹(源文件中定义wp-includes\option.php
和wp-includes\plugin.php
),这两个功能在这些文件是有效的,代码块两种以上是在我的插件相同的文件,我不包括或需要的任何文件。
Anyone has a clue?
有人有线索吗?
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.
正如所问,我调用的完整代码块来自get_option
我的类的构造函数,它包装了插件。
function __construct() {
global $wpdb;
$this->table_iso = $wpdb->prefix . "ntp_iso";
$this->pluginUrl = get_option('siteurl') . '/wp-content/plugins/my-plugin';
}
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:
也可能值得一提:我有一个包装实际插件的类,在那个 .php 文件的底部,我有(在类定义之外),这段代码:
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'));
}
These all work like a charm.
这些都像一个魅力。
采纳答案by Ray
I suspect wp-includes\option.php is not being loaded.
我怀疑 wp-includes\option.php 没有被加载。
Just for grins, right before the call to get_options() add
只是为了咧嘴笑,就在调用 get_options() 之前添加
include_once('wp-includes\option.php');
include_once('wp-includes\option.php');
Or try calling something else in option.php like: update_option(null);
或者尝试在 option.php 中调用其他内容,例如: update_option(null);
option.php
is included from inside wp-includes/functions.php
while plugin.php
gets included in one of several different places.
option.php
从内部包含,wp-includes/functions.php
而plugin.php
包含在几个不同的地方之一。
You can see all the files that are currently included/required by inserting this into your code:
您可以通过将其插入代码中来查看当前包含/需要的所有文件:
$includedStuff = get_included_files();
print_r($includedStuff);
Good Luck!
祝你好运!
回答by Sumith Harshan
I got the same Fatal Error and then I loaded the wp-config.php file with relevant path.
我遇到了同样的致命错误,然后我用相关路径加载了 wp-config.php 文件。
My file was wp-content/plugins/myplugin/css/mystyle.php
我的文件是wp-content/plugins/myplugin/css/mystyle.php
I added following code at the top of the page. Then get_option()function worked perfectly.
我在页面顶部添加了以下代码。然后get_option()函数完美运行。
require_once('../../../../wp-config.php');
May be this would be help.
可能这会有所帮助。
Regards
问候
回答by fisicx
You can test to see if options.php is working:
您可以测试以查看 options.php 是否正常工作:
http://yourdomain.com/wp-admin/options.php
http://yourdomain.com/wp-admin/options.php
If your options have been saved then they will be listed.
如果您的选项已保存,则它们将被列出。
But...
但...
add_options_page is for the dashboard menu.
add_options_page 用于仪表板菜单。
What you need is add_option in order to use get_option
您需要的是 add_option 才能使用 get_option
回答by Nicolas Wynkoop
I just wanted to add a note here for anyone getting the "PHP Fatal error: Call to undefined function apply_filter" error. I did a google search for this error and found this question.
我只是想在这里为任何收到“PHP 致命错误:调用未定义的函数 apply_filter”错误的人添加注释。我用谷歌搜索了这个错误,发现了这个问题。
NOTE:You may need to check your function spelling, to make sure you have "apply_filters", plural, not "apply_filter" singular, as this was my problem (which lead me to this post!).
注意:你可能需要检查你的函数拼写,以确保你有“apply_filters”,复数,而不是“apply_filter”单数,因为这是我的问题(这让我看到了这篇文章!)。
回答by Van Leo Adrivan
I used Sumith's Code but edited it a little - It was the only code that worked amongst the codes mentioned here.
我使用了 Sumith 的代码,但对其进行了一些编辑 - 这是此处提到的代码中唯一有效的代码。
Here's mine:
这是我的:
require_once(dirname(__FILE__).'../../../../wp-config.php');