admin管理员组文章数量:1277896
I've written a simple Wordpress plugin to create a new database table. The new table ought to be created when the plugin is activated. When I try to activate the plugin, I get the following error:
The plugin generated 3989 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.
emphasized text This is obviously a result of the fact the $wbdb is not defined. xDebug outputs the following:
[Mon Feb 04 ] [error] PHP Notice: Undefined variable: wpdb in test.php on line 13
The entire plugin consists of the following:
<?php
/**
* Plugin Name: Test Plugin
* Plugin URI:
* Description: A test plugin.
* Version: 1.0
* Author: Christopher Green
* Author URI:
*/
$test_db_name = $wpdb->prefix . 'test_db_name';
function test_install_plugin() {
global $wpdb;
global $test_db_name;
$sql = "CREATE TABLE " . $test_db_name . " (
`id` int(9) NOT NULL AUTO_INCREMENT,
UNIQUE KEY id (id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
register_activation_hook(__FILE__,'test_install_plugin');
?>
I have no other plugins installed. Why isn't $wpdb defined? Is there a standard way to create a new database table when activating a plugin?
I've written a simple Wordpress plugin to create a new database table. The new table ought to be created when the plugin is activated. When I try to activate the plugin, I get the following error:
The plugin generated 3989 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.
emphasized text This is obviously a result of the fact the $wbdb is not defined. xDebug outputs the following:
[Mon Feb 04 ] [error] PHP Notice: Undefined variable: wpdb in test.php on line 13
The entire plugin consists of the following:
<?php
/**
* Plugin Name: Test Plugin
* Plugin URI: http://everybytcaptive
* Description: A test plugin.
* Version: 1.0
* Author: Christopher Green
* Author URI: http://everybytecaptive
*/
$test_db_name = $wpdb->prefix . 'test_db_name';
function test_install_plugin() {
global $wpdb;
global $test_db_name;
$sql = "CREATE TABLE " . $test_db_name . " (
`id` int(9) NOT NULL AUTO_INCREMENT,
UNIQUE KEY id (id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
register_activation_hook(__FILE__,'test_install_plugin');
?>
I have no other plugins installed. Why isn't $wpdb defined? Is there a standard way to create a new database table when activating a plugin?
Share Improve this question edited Feb 10, 2013 at 14:55 cg433n asked Feb 4, 2013 at 19:17 cg433ncg433n 1235 bronze badges3 Answers
Reset to default 2$wpdb
is outside the scope of your plugin file, you need global $wpdb;
before using $wpdb->prefix
Your plugin is trying to access $wpdb before it exists. You need to wrap it in an action hook, like so:
<?php
/**
* Plugin Name: Test Plugin
* Plugin URI: http://everybytcaptive
* Description: A test plugin.
* Version: 1.0
* Author: Christopher Green
* Author URI: http://everybytecaptive
*/
add_action( 'init', 'test_install_plugin' );
function test_install_plugin() {
global $wpdb;
global $test_db_name;
$test_db_name = $wpdb->prefix . 'test_db_name';
$sql = "CREATE TABLE " . $test_db_name . " (
`id` int(9) NOT NULL AUTO_INCREMENT,
UNIQUE KEY id (id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// commented out for now, worry about running activation later
// register_activation_hook(__FILE__,'test_install_plugin');
I highly recommend devouring everything you can find with Google relating to actions and filters in WordPress. They are core concepts that you need to fully understand in order to create plugins and themes effectively.
Well, in my case the issue was due to a duplicate db_table
creation process, and when I added an if statement
to check whether the db_table
exists or not the issue was resolved.
Here is how:
define( 'TABLEName', $wpdb->prefix . 'test_table' );
if( $wpdb->get_var( "show tables like '" . TABLEName . "'" ) != TABLEName ) {
create table;
}
Now the plugin checks if the required table(s) exists or not before activation, if not exist then it will create it, if exists will escape the creation.
本文标签: databaseThe plugin generated x characters of unexpected outputwpdb not defined
版权声明:本文标题:database - The plugin generated x characters of unexpected output, $wpdb not defined 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741260423a2367514.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论