Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress: PHP Fatal error: Call to undefined function get_option()

Tags:

php

wordpress

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.

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

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);

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.

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.

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:

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.

like image 255
testuser Avatar asked Oct 11 '13 20:10

testuser


5 Answers

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.

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!).

like image 84
Nicolas Wynkoop Avatar answered Oct 18 '22 09:10

Nicolas Wynkoop


I used Sumith's Code but edited it a little - It was the only code that worked amongst the codes mentioned here.

Here's mine:

require_once(dirname(__FILE__).'../../../../wp-config.php'); 
like image 28
Van Leo Adrivan Avatar answered Sep 28 '22 10:09

Van Leo Adrivan


I got the same Fatal Error and then I loaded the wp-config.php file with relevant path.

My file was wp-content/plugins/myplugin/css/mystyle.php

I added following code at the top of the page. Then get_option() function worked perfectly.

require_once('../../../../wp-config.php'); 

May be this would be help.

Regards

like image 12
Sumith Harshan Avatar answered Oct 18 '22 11:10

Sumith Harshan


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

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

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

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

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);

Good Luck!

like image 9
Ray Avatar answered Oct 18 '22 11:10

Ray


You can test to see if options.php is working:

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.

What you need is add_option in order to use get_option

http://codex.wordpress.org/Function_Reference/add_option

like image 1
fisicx Avatar answered Oct 18 '22 10:10

fisicx