Wordpress global $wpdb function issue

感情迁移 提交于 2019-12-24 07:25:53

问题


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>

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");
?>

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 

I include this line above the "global $wpdb"

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

But nothing happen

My file path structure is

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

Any idea.


回答1:


Add this line before global $wpdb

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

OR

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


来源:https://stackoverflow.com/questions/24773312/wordpress-global-wpdb-function-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!