Wordpress全局$ wpdb函数问题

时间:2014-07-16 06:03:48

标签: php wordpress wordpress-plugin wpdb

我正在开发插件,在插件中我想在新表中添加数据。我在这个创建文件

中创建了一个文件“lsp_manage_foo.php”
<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”中

<?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”文件并显示此错误

    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 

我在“global $ wpdb”

上面加上这一行
include "../../../../wp-includes/wp-db.php";

但没有任何事情发生

我的文件路径结构是

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

任何想法。

1 个答案:

答案 0 :(得分:2)

global $wpdb

之前添加此行
require_once( str_replace('//','/',dirname(__FILE__).'/') .'../../../wp-config.php');

OR

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