doulai7239 2014-07-16 06:03
浏览 50
已采纳

Wordpress全局$ wpdb函数问题

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条回答 默认 最新

  • dqyknf4423 2014-07-16 06:18
    关注

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本