1

This is my first go at WordPress plugin development and any help you folks can give me would be appreciated.

This plugin needs to keep careful track of how users interact with certain areas of it. I have a table to log the data. I have written this function (defined in the plugin's class):


 public function update_history($userid,$idevent,$eventdesc,$log){
     //get a database object
     echo "we got here";

     global $wpdb;
     $wpdb->show_errors();

     $table_name = $wpdb->prefix . 'pw_history_log';
     //values to write to database
     $item = array(
         'iduser' => $userid,
         'ieventcode' => $idevent,
         'dtevent' => date("Y-m-d H:i:s"),
         'szactiondesc' => $eventdesc,
         'siteid' => $log
     );
     // format values
     $format = array('%d','%d','%s','%s','%d');
     // Insert the data
     $wpdb->insert($table_name, $item, $format);
}

I'm trying to call it elsewhere like this:

do_action( 'update_history', 333, 2001, "test event", 1 );
add_action( 'init', 'update_history', 10, 4 );

I'm not getting errors. I'm not getting data in the database. I'm not getting any sign of the silly echo statement I tossed in, either. So...the function isn't running.

Can anyone point me in the right direction? I'm feeling really stupid here.

EDIT: I think I'm not instantiating the admin class right. I took out the lines where I was trying to call the function and added:

$plugin_name_admin = new Plugin_Admin("name", "1.0");
$plugin_name_admin->update_history(333, 2001, "test event", 1);

and now it is working!

  • Of course now it is posting multiple entries at once into the table...but hey, it's an improvement! – Deleyna Jul 24 '18 at 11:27
0

Just change the action hook to:

add_action( 'update_history', 'update_history', 10, 4 );

It will work.

  • Thank you for that. I'd made a bit more fundamental of a mistake. New to OOP and using someone else's boilerplate...missed a basic function. – Deleyna Jul 24 '18 at 11:26
  • Glad to know that, if it works for you, please accept the answer. – Baikare Sandeep Jul 24 '18 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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