本文介绍了Wordpress 全局 $wpdb 函数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个插件,在插件中我想在新表中添加数据.我在这个文件中创建了一个文件lsp_manage_foo.php"

I am working on a plugin, in plugin i want to add data in new table. I create a file "lsp_manage_foo.php" in this file i create

<form action="<?php echo plugin_dir_url(__FILE__) ?>lsp_foo/lsp_manage_process.php" method="post" name="lsp_add_foo">
    <table width="100%" border="0" cellspacing="4" cellpadding="0">
        <tr>
            <td width="25%">
                  <label>
                       foo Name
                  </label>
            </td>
            <td width="58%">
                 <input type="text" name="lsp_add_foo" id="lsp_add_foo" value="">
            </td>
            <td width="10%" align="right">
                 <input type="submit" name="lsp_save_foo" value="Add Foo">
            </td>
         </tr>
     </table>
</form>

在lsp_manage_process.php"中

And in "lsp_manage_process.php"

<?php

    global $wpdb;
    $foo_add = $wpdb->prefix."lsp_foo";

    $lsp_foo_name = stripslashes(strip_tags($_POST['lsp_add_foo']));

    $foo_shortcode = str_replace(" ", "_", $lsp_foo_name);
    $foo_shortcode = strtolower($foo_shortcode);

    $foo_data = array(
        'foo_name'       =>  $lsp_foo_name,
        'foo_shortcode'  =>  $foo_shortcode
    );

    $foo_insert = $wpdb->insert($foo_add,$foo_data);

    header("Location: lsp_manage_foo.php");
?>

我遇到的问题是当我点击提交时,它会转到lsp_manage_process.php"文件并显示这个错误

The issue i have faced is When i click on submit it goes to "lsp_manage_process.php" files and show me this error

    Notice: Trying to get property of non-object in /var/www/dev/lgs_pro/wp-content/plugins/foo_pro/lsp_foo/lsp_manage_process.php on line 5 
Fatal error: Call to a member function insert() on a non-object in /var/www/dev/lgs_pro/wp-content/plugins/foo_pro/lsp_foo/lsp_manage_process.php on line 18 

我将此行包含在全局 $wpdb"上方

I include this line above the "global $wpdb"

include "../../../../wp-includes/wp-db.php";

但是什么都没发生

我的文件路径结构是

wp-content/plugins/foo_pro/lsp_foo/lsp_manage_process.php

任何想法.

推荐答案

global $wpdb

require_once( str_replace('//','/',dirname(__FILE__).'/') .'../../../wp-config.php');

 require_once( str_replace('//','/',dirname(__FILE__).'/') .'../../../../wp-config.php');

这篇关于Wordpress 全局 $wpdb 函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 23:11