0

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

3
  • change $wpdb->custom to 'wp_custom' and see what will happen.
    – revo
    Aug 30, 2013 at 7:36
  • 1
    You need to make sure that you have set the $wpdb->custom variable; this is not automatic. You should use $wpdb->prefix . 'custom' to build the table name with the proper database prefix
    – shea
    Aug 30, 2013 at 8:46
  • Hey thanx for the answer. Working now. Aug 30, 2013 at 11:56

1 Answer 1

0

$wpdb->custom should be "{$wpdb->prefix}custom"

$wpdb->insert( $table, $data, $format ); take three params where 1st is table name. you should avoid wp as its not necessary that prefix will always be wp_ instead use $wpdb->prefix to get table prefix, "{$wpdb->prefix}custom" will output wp_custom (given prefix is wp for your site)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.