I am new to development so any feedback is appreciated - i have built a wordpress plugin that needs to create a new database table on plugin activation. When I activate the plugin i get the following error, and the new table has not been created.
'The plugin generated 1050 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.'
Here's the code:
global $ed_wp_cardealer_db_version;
$ed_wp_cardealer_db_version = '1.0';
//create database table
function create_database(){
global $wpdb;
global $ed_wp_cardealer_db_version;
$table_name = $wpdb->prefix . 'ed_wp_cardealer';
$charset = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
valuation_id int NOT NULL AUTO_INCREMENT,
vrm TINYTEXT,
price int(9),
vehicleDescription TINYTEXT,
email TINYTEXT,
PRIMARY KEY (valuation_id)
)$charset;";
require_once(ABSPATH . "wp-admin/includes/upgrade.php");
dbDelta( $sql );
add_option( 'ed_wp_cardealer_db_version', $ed_wp_cardealer_db_version );
}
//create database on plugin activation
register_activation_hook( __FILE__, 'create_database' );
I've checked it over and over against the wordpress official docs and can't see any obvious errors. If I activate the plugin without this line then there is no error, but the database is still not created
register_activation_hook( __FILE__, 'create_database' );
I have seen lot's of responses to similar queries that suggest removing extra space from opening php tag etc, but I need to create the new table as well as remove the error.
I have set debug to true in wp-config, but can't see any further details than i have given.
I change valuation_id int value length. When you declare int value in DB must assign int length. Also, add two extra parameters on add_option.
Please try the below code hopefully work it. I test it on my local development server.
global $ed_wp_cardealer_db_version;
$ed_wp_cardealer_db_version = '1.0';
//create database table
function create_database(){
global $wpdb;
global $ed_wp_cardealer_db_version;
$table_name = $wpdb->prefix . 'ed_wp_cardealer';
$charset = $wpdb->get_charset_collate();
$sql = " CREATE TABLE $table_name (
valuation_id int(11) NOT NULL AUTO_INCREMENT,
vrm TINYTEXT,
price int(9),
vehicleDescription TINYTEXT,
email TINYTEXT,
PRIMARY KEY ( valuation_id )
)$charset;";
require_once( ABSPATH . "wp-admin/includes/upgrade.php" );
dbDelta( $sql );
add_option( 'ed_wp_cardealer_db_version', $ed_wp_cardealer_db_version, '', 'yes' );
}
//create database on plugin activation
register_activation_hook( __FILE__, 'create_database' );
Screenshot:
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.