Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

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

<?php
/*
Plugin Name: Contact Form 7
Plugin URI: http://contactform7.com/
Description: Just another contact form plugin. Simple but flexible.
Author: Takayuki Miyoshi
Author URI: http://ideasilo.wordpress.com/
Text Domain: wpcf7
Domain Path: /languages/
Version: 3.1.2
*/

WP will detect the plugin automagically. Then simply define/require/include what you need. The plugin will then be loaded just before the plugins_loaded hook, which is the first one available to you. You then can use the complete WP environment.

share|improve this answer
    
thanks for the reply, I wasn't specific enough in my question. the pluginname/submit/pick.php is just one of multiple files in my plug-in. It is called through AJAX to insert something in the database. –  Joe_Schmoe Apr 24 '12 at 12:31
    
Here's the archive to make it right: ajax. PLUS Edit your Q to reflect that fact. –  kaiser Apr 24 '12 at 12:38
    
Thanks I'll look into that tonight and see if it works. –  Joe_Schmoe Apr 24 '12 at 12:44
up vote 0 down vote accepted

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

// load your javascript, and setup the ajaxurl variable
// this is loading my js everywhere, we could load it only where needed
// 0.01 is the version of your js, increment this each time you change your js
//   so that you don't keep using the same cached version
wp_enqueue_script( 'pluginname', plugins_url( 'js/pluginname.js', __FILE__ ), array( 'jquery' ), 0.01 );
wp_localize_script( 'pluginname', 'pluginname', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );

// wp_ajax_insert_pick is a combination of wp_ajax_ and the 'action' : 'insert_pick' you provide in your js
// ajax_insert_pick is the function below that is called to handle your ajax request 
add_action( "wp_ajax_action_name", "ajax_insert_pick" );

// this basically replaces pluginname/submit/pick.php
function ajax_insert_pick() {
    global $wpdb;
    $var = $_POST['var'];
    // handle the ajax request here
    echo $response;
    die(); // needed otherwise wordpress may append a zero to your response.
}

in pluginname/js/pluginname.js

function submit_pick( var ) {
    jQuery.post( pluginname.ajaxurl, 
                 { 'action' : 'insert_pick',
                   'var' : var },
                 function(response) {
                     alert(response); },
                 'text' );
}

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.