To clarify my question…
My plugin is/was using AJAX to call the file pluginname/submit/pick.php
My pluginname/pluginname.php
contains the usual plugin header and wordpress automatically finds it.
Original Question…
I’m trying to use the global $wpdb
in a plugin to insert data into one of my tables.
From the docs it sounds like I just need to include wp-blog-header.php
However when I try to do so I’m getting an error.
PHP Fatal error: [
require()
](function.require) :
Failed opening required ‘http://localhost:8888/blog/wp-blog-header.php’ (include_path=’.:/Applications/MAMP/bin/php/php5.3.6/lib/php’) in /Applications/MAMP/htdocs/blog/wp-content/plugins/pluginname/submit/pick.php on line 4
My pluginname/submit/pick.php
(which is called by AJAX) has the following code
<?php
$p = 'http://localhost:8888/blog/wp-blog-header.php';
echo $p;
require($p);
echo 'hi';
?>
If I load the pick.php
, I see http://localhost:8888/blog/wp-blog-header.php
and that is it. So it must be failing on the require, as the error log confirms.
The path is correct as I inserted a quick echo into wp-blog-header.php
, and copy pasted the output from pick.php
into the address bar and it worked.
Any help would be appreciated.
You’re making something completely wrong.
Header comment
In your main file, you need the following comment on top (ex. taken from Contact form 7):
WP will detect the plugin automagically. Then simply
define/require/include
what you need. The plugin will then be loaded just before theplugins_loaded
hook, which is the first one available to you. You then can use the complete WP environment.The short answer
The error is because I put a URL in instead of a filepath. and the one line fix is to set
$p = '../../../../wp-blog-header.php';
The long answer
Following kaiser’s link led me down a long road that eventually lead to what I believe is the correct way to do this.
in
pluginname/plugin.php
in
pluginname/js/pluginname.js
Since I just learned this last night, and am only a month into learning php/mysql/wp/js/ajax there are quite possibly some errors in the above. But it worked for me last night, so I’m happy with it at this stage.