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.

I am using the Jeditable plugin to enable inline editing on a table inside the admin area, generated by a small plugin I've written. (http://www.appelsiini.net/projects/jeditable). Basically everything is working, but only as long as the code for updating the database field is inside the plugin's code. As soon as I copy that code into another file and try to edit something, I get this error:

Fatal error: Call to a member function update() on a non-object in XXX on line XX

I only understand partly why I get that error. Is it because $wpdb is not initialized? I tried global $wpdb without luck. That's why I think I am missing something, somewhere in the code $wpdb gets "lost" somehow, so the update method can't be found. It is really frustrating, because the code is working, but as soon as I use it in a seperate file, it's not working. If needed I can post the code, but I am hoping it's just a standard issue.

EDIT: Ok, so here is my code:

That is the Jeditable plugin in action, right after the table (Let's say it's inside the index.php):

<script>
    jQuery(document).ready(function() {
        jQuery('.edit').editable('../wp-content/plugins/myplugin/save.php', {
            indicator   :   'Saving...',
            tooltip     :   'Please click to edit!',
            id          :   'my_id',
            name        :   'my_value',
            width       :   'none',
            cssclass    :   'editable-input'
        });
    });
</script>

As you can see, I am calling the save.php which looks like this:

if(isset($_POST['my_id']))
{
    error_reporting (E_ALL);

    $explode    =   explode('-', $_POST['my_id']);
    $my_row =   $explode[0];
    $my_id      =   $explode[1];
    $my_value   =   $_POST['ck_value'];

    if($my_row == 'date') {

        $exp = explode(' ', $my_value);

        switch($exp[1]) {
            case 'Jan': case 'jan': case 'Januar': case 'januar': case '01': case '1':
                $month = '01';
                $montha = 'Jan';
                break;
            case 'Feb': case 'feb': case 'Februar': case 'februar': case '02': case '2':
                $month = '02';
                $montha = 'Feb';
                break;
        }


        $day = substr($exp[0], 0, 2);
        $wpdb->update($table_name, array('ck_date' => $exp[2].'-'.$month.'-'.$day, 'ck_monthabbr' => $montha) , array('ck_id' => $my_id));
        echo $exp[2].'-'.$month.'-'.$day.'<br />';
    }

    if($my_row == 'ogo') {
        $wpdb->update($table_name, array('ck_geo' => $my_value) , array('ck_id' => $my_id));
    }

    if($my_row == 'vue') {
        $wpdb->update($table_name, array('ck_venue' => $my_value) , array('ck_id' => $my_id));
    }
}

Inside the index.php, I have global $wpdb and $table_name defined, but save.php doesn't recognize these variables.

share|improve this question
    
I do believe we may need to see your code, although somebody else may be able to answer it. (?) –  Jared Jan 21 '12 at 15:41
add comment

1 Answer 1

up vote 0 down vote accepted

I think you will need to include some type of WP core file into the save.php file (I could be wrong). Since this file isn't being called specifically by your plugin (or WP for that matter), it won't have all of the variables that WP has to offer.

Try adding this to the top of your save.php file:

require_once '../../../../wp-load.php';

There may be better ways to get the URL, and even maybe a better file to use than wp-load.php, but at least to verify it works that way will probably relieve some stress.

You may also want to use global $wpdb; in save.php after wp-load.php is included.

share|improve this answer
    
Sorry to disappoint you, it's still not working. –  Sven Jan 21 '12 at 16:30
    
@Sven The issue is that your save.php file doesn't know what $wpdb is. We can at least conclude that it is a scope issue. Possibly the URL to wp-load.php is going back too far or not far enough? Just a thought, and could be a possibility. –  Jared Jan 21 '12 at 18:17
1  
Ok, got it working, thank you! wp-blog-header.php is the file that needs to be included and I was working in the wrong file. Well, I am a beginner ;-) –  Sven Jan 21 '12 at 19:35
    
We all start somewhere :) Glad you got it figured out! –  Jared Jan 21 '12 at 19:45
    
More importantly, instead of include you may want to use require_once. To quote the PHP manual, "include() produces a Warning while require() results in a Fatal Error. Fatal errors stop script execution." Source: codex.wordpress.org/… –  Jared Jan 21 '12 at 20:06
add comment

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.