Use of any code provided by Harmonic Design or HDPlugins.com is at your own risk. It is ALWAYS recommended to backup your site or files before modifying any code on your site. Copy Code

// Automatically place a "Retry quiz" button to quiz results
function hd_results_after_content($quiz)
{
?>
    <p style="text-align: right;"><a href="<?php echo get_the_permalink(); ?>" id = "hdq_retry_link">Retry Quiz</a></p>
<?php
}
add_action("hdq_results_after_content", "hd_results_after_content", 10, 1);


function hdq_before_Rich($quizID)
{
?>
	<script type="text/javascript">
		/* modify these to reflect the IDs of your fields */
		const HDQ_HDF_FIELDS = [
			"hdf_text_feycq5",
			"hdf_email_2o23b5"
		];

		function hdq_custom_init_Rich() {
			const URLParams = new URLSearchParams(document.location.search);

			setTimeout(function() {
				// get data from URL
				let data = [];
				for (let i = 0; i < HDQ_HDF_FIELDS.length; i++) {
					data[HDQ_HDF_FIELDS[i]] = URLParams.get(HDQ_HDF_FIELDS[i]);
				}

				// if data exists, set the form
				for (let k in data) {
					if (data[k]) {
						document.getElementById(k).value = data[k];
					}
				}
			}, 1000); // give the form time to initialize			
		}

		function hdq_custom_submit_Rich() {
			const permalink = hdq_custom_submit_create_url(HDQ.VARS.quiz.permalink);
			document.getElementById("hdq_retry_link").setAttribute("href", permalink);

			// check the score. Only redirect is quiz score is 100%
			if (HDQ.VARS.hdq_score[0] != HDQ.VARS.hdq_score[1]) {
				HDQ.VARS.quiz.quiz_redirect_url = "";
			} else {
				const u = hdq_custom_submit_create_url(HDQ.VARS.quiz.quiz_redirect_url);
				HDQ.VARS.quiz.quiz_redirect_url = u;
			}
			return '{}'; // expects a JSON string to be returned
		}

		function hdq_custom_submit_create_url(url) {
			let u = new URL(url);
			let formData = HDF.VARS["hdf-" + hdq_srp_hdf].data;

			// create the URL paramaters
			for (let i = 0; i < HDQ_HDF_FIELDS.length; i++) {
				if (formData[HDQ_HDF_FIELDS[i]]) {
					u.searchParams.append(HDQ_HDF_FIELDS[i], formData[HDQ_HDF_FIELDS[i]].value);
				}
			}
			return u;
		}
	</script>
<?php
}
add_action('hdq_before', 'hdq_before_Rich');


// Tell HD Quiz to run custom JS functions on quiz completion
function hdq_submit_Rich($quizOptions)
{
	array_push($quizOptions->hdq_submit, "hdq_custom_submit_Rich");
	array_push($quizOptions->hdq_init, "hdq_custom_init_Rich");
	return $quizOptions;
}
add_action('hdq_submit', 'hdq_submit_Rich');