Skip to content

Instantly share code, notes, and snippets.

@verygoodplugins
Last active April 13, 2020 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save verygoodplugins/8df7f69e5862797723909577edf254f2 to your computer and use it in GitHub Desktop.
Save verygoodplugins/8df7f69e5862797723909577edf254f2 to your computer and use it in GitHub Desktop.
Sync LearnDash quiz results to custom fields in the CRM
<?php
// Register the quiz fields in the WPF settings
function wpf_add_learndash_quiz_fields( $meta_fields ) {
$args = array(
'post_type' => 'sfwd-quiz',
'nopaging' => true,
);
$quizzes = get_posts( $args );
foreach ( $quizzes as $quiz ) {
$meta_fields[ 'quiz_' . $quiz->ID . '_pass' ] = array(
'label' => $quiz->post_title . ' - Pass',
'type' => 'checkbox',
'group' => 'learndash_progress',
);
$meta_fields[ 'quiz_' . $quiz->ID . '_fail' ] = array(
'label' => $quiz->post_title . ' - Fail',
'type' => 'checkbox',
'group' => 'learndash_progress',
);
}
return $meta_fields;
}
add_filter( 'wpf_meta_fields', 'wpf_add_learndash_quiz_fields' );
// Sync the pass / fail data to the custom fields
function wpf_quiz_completed( $data, $user ) {
if ( isset( $data['quiz']->ID ) ) {
$quiz_id = $data['quiz']->ID;
} else {
// For grading in the admin
$quiz_id = $data['quiz'];
}
if ( true == $data['pass'] ) {
$update_data = array(
'quiz_' . $quiz_id . '_pass' => true,
'quiz_' . $quiz_id . '_fail' => false,
);
} elseif ( false == $data['pass'] ) {
$update_data = array(
'quiz_' . $quiz_id . '_pass' => false,
'quiz_' . $quiz_id . '_fail' => true,
);
}
wp_fusion()->user->push_user_meta( $user->ID, $update_data );
}
add_action( 'learndash_quiz_completed', 'wpf_quiz_completed', 5, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment