Wordpress global $wpdb function issue

2019-08-03 09:17发布

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条回答
走好不送
2楼-- · 2019-08-03 09:46

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');
查看更多
登录 后发表回答