PHP global $post not working! Trying to get the post ID in WordPress

I’m running into an issue where i’m trying to get the Post ID while trying to hook into a plugin function(function is inside my functions.php file)

For some reason global $post is not working!

Read More

My objective is to store the current posts, post ID into a variable where after the quiz is done, it will update a custom field.

Here is my code below. I’m echoing the $post->ID; just to test, but I’m not getting anything back.

add_action('ssquiz_finished', 'after_quiz_done', 10, 4);

function after_quiz_done( $quiz_id, $user_id, $questions_right, $total_questions )
{
    global $post;

    echo $post->ID;

}

Here is the actual ‘ssquiz_finished’ function/hook from the SS QUIZ plugin:

$output = ob_get_contents();
ob_end_clean();
$finish_screen .= $output;

$temp = new stdClass();
$temp->user_name = $info->user->name;
$temp->user_email = $info->user->email;
$temp->time_spent = time() - $info->started;
$wpdb->insert("{$wpdb->base_prefix}ssquiz_history", 
                array( 
                    'user_id' => $info->user->id,
                    'quiz_id' => $info->quiz->id,
                    'meta' => serialize( $temp ),
                    'answered'=> $info->questions_counter,
                    'correct' => $info->questions_right,
                    'total' => $info->total_questions
                ), 
                array( '%d', '%d', '%s', '%d', '%d', '%d' ) );

if( $info->questions_counter == $info->total_questions ) {
    // API
    $percent = intval(strval($info->questions_right / $info->questions_counter * 100 ) ); //$info->questions_counter !=1
    do_action( 'ssquiz_finished', $info->quiz->id, $info->user->id, $info->questions_right, $info->total_questions );

Any help would be greatly appreciated.

Related posts

Leave a Reply