0

I'm trying to build (my first) a basic WordPress plugin that stores form data and one of the options I need is the ability to delete old form data. I've been experimenting with trying to get a simple query to work, but I keep getting an error message. The error reads ' Fatal error: Call to a member function query() on a non-object in /home/.. on line 5'

So in theory when I click submit it should delete the post. Am I way off? Any help is greatly appreciated.

Plugin admin page -

<h1>Form Data</h1>


<form action="next.php" method="post">

<input type="submit">


</form>

Next.php

<?php

global $wpdb;

$wpdb->query( $wpdb->prepare( "DELETE FROM wp_posts WHERE ID = 42" ) );

?>
4

2 回答 2

0

如果您按原样运行代码。它不会起作用,因为您没有包含来自 Wordpress 的任何支持 php 文件。所以 $wpdb 没有定义,因此你调用它的任何函数也没有定义。

看起来您正在错误地进行 Wordpress 插件开发。在他们的网站上查看他们的文档和 API。

于 2013-07-10T23:21:27.750 回答
0
<?php

// Pulls in WordPress prerequisite code
include_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php' );

// Bring global variable $wpdb into local scope
global $wpdb;

// Run the database query
$wpdb->query( $wpdb->prepare( "DELETE FROM wp_posts WHERE ID = 42" ) );

?>
于 2014-03-09T17:15:15.703 回答