I am currently learning WordPress Plugin Development. I was trying to insert data into a custom table by means of a plugin. Such that, when an action hook is fired, the data is inserted. It is a pretty simple code here but not getting executed although on debugging I found that the commented echo statement within the function does get executed.
Here is the code -
<?php
/*
Plugin Name: What the wpdb
Description: Making use of wpdb
Version: 1.0
Author: Navin Nagpal
License: GPLv2
*/
add_action('wp_head','nn_do_dbstuff');
function nn_do_dbstuff(){
global $wpdb;
//echo 'Text';
$values = array(
'column1'=>'Navin',
'column2'=>22);
$formats_values = array('%s','%d');
$wpdb->insert($wpdb->custom,$values,$formats_values);
}
?>
My table is named wp_custom and it has three fields including the 'id' field which is the primary key
$wpdb->custom
to'wp_custom'
and see what will happen.$wpdb->custom
variable; this is not automatic. You should use$wpdb->prefix . 'custom'
to build the table name with the proper database prefix