Modifying values with add_action to be sent to db

I am struggling with this problem for hours now, what is going on is that somewhere before inserting the values into the db, this action is being called.

do_action_ref_array(‘h5p_alter_user_result’, array(&$data, $result_id, $content_id, $user_id));

Following are the values and how they are being populated.

$user_id = get_current_user_id();
$result_id = $wpdb->get_var($wpdb->prepare(
“SELECT id
FROM {$wpdb->prefix}h5p_results
WHERE user_id = %d
AND content_id = %d”,
$user_id,
$content_id
));

$table = $wpdb->prefix . ‘h5p_results’;
$data = array(
‘score’ => filter_input(INPUT_POST, ‘score’, FILTER_VALIDATE_INT),
‘max_score’ => filter_input(INPUT_POST, ‘maxScore’, FILTER_VALIDATE_INT),
‘opened’ => filter_input(INPUT_POST, ‘opened’, FILTER_VALIDATE_INT),
‘finished’ => filter_input(INPUT_POST, ‘finished’, FILTER_VALIDATE_INT),
‘time’ => filter_input(INPUT_POST, ‘time’, FILTER_VALIDATE_INT)
);
if ($data[‘time’] === NULL) {
$data[‘time’] = 0;
}
$format = array(
‘%d’,
‘%d’,
‘%d’,
‘%d’,
‘%d’
);

$content_id = 1;

what I am trying to do is that I am wanting to modify these values before they are being saved in the database ( and yes this hook is called before saving in db ), for this I have created the following functionality in my plugin.

add_action( ‘h5p_alter_user_result’, ‘diff_test_callback’ );

function diff_test_callback($args)
{
global $wpdb;
$user_id = 1;
$result_id = 1;
$content_id = 1;
$table = $wpdb->prefix . ‘h5p_results’;
$data = array(
‘score’ => 777,
‘max_score’ => 189,
‘opened’ => 333,
‘finished’ => 222,
‘time’ => 111
);
if ($data[‘time’] === NULL) {
$data[‘time’] = 0;
}
$args = array( $data, $result_id, $content_id, $user_id );
return $args;
}

But the values are not being modified, is there some thing wrong?

Read more here:: Modifying values with add_action to be sent to db

Leave a Reply

Your email address will not be published. Required fields are marked *