Chained Quiz Shortcode

Below, you’ll find a detailed guide on how to add the Chained Quiz Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Chained Quiz Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Chained Quiz Plugin and the shortcodes it provides:

Plugin Icon
Chained Quiz

"Chained Quiz is a powerful WordPress plugin designed to create engaging and dynamic quizzes. It allows users to build sequential or branching quizzes, enhancing visitor engagement on your website."

★★★★☆ (38) Active Installs: 2000+ Tested with: 6.3.2 PHP Version: 7.4
Included Shortcodes:
  • [chained-share]

Chained Quiz [chained-share] Shortcode

The Chained-Quiz plugin shortcode is used to manage and display quizzes on your WordPress site. It selects a quiz, checks for completion ID, and presents the first question along with possible answers. The shortcode also handles text captcha requirements, ensuring user interaction is genuine. It then loads the quiz UI, providing a seamless experience for the users.

Shortcode: [chained-share]

Examples and Usage

Basic example – Displays the chained quiz by referencing the quiz ID.

[chained-share id=1 /]

PHP Function Code

In case you have difficulties debugging what causing issues with [chained-share] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('chained-share', array("ChainedSharing", "display"));

Shortcode PHP function:

function display($quiz_id) {
	   global $wpdb, $user_ID, $post;
	   $_question = new ChainedQuizQuestion();
	   
	   // select the quiz
	   $quiz = $wpdb -> get_row($wpdb->prepare("SELECT * FROM ".CHAINED_QUIZZES." WHERE id=%d", $quiz_id));
	   if(empty($quiz->id)) die(__('Quiz not found', 'chained'));
	   
	   // completion ID already created?
		if(empty($_COOKIE['chained_completion_id'.$quiz->id])) {			
			$wpdb->query( $wpdb->prepare("INSERT INTO ".CHAINED_COMPLETED." SET
		 		quiz_id = %d, datetime = NOW(), ip = %s, user_id = %d",
		 		$quiz->id, chained_user_ip(), $user_ID));
		 	?>
		 		<script type="text/javascript" >
			  	var d = new Date();
				d.setTime(d.getTime() + (24*3600*1000));
				var expires = "expires="+ d.toUTCString();     				
			  	document.cookie = "chained_completion_id<?php echo $quiz->id?>=<?php echo $wpdb->insert_id;?>;" + expires + ";path=/";
			  	</script>
			<?php  		 	
		}
	   
		 // select the first question
		 $question = $wpdb->get_row($wpdb->prepare("SELECT * FROM ".CHAINED_QUESTIONS." WHERE quiz_id=%d
		 	ORDER BY sort_order, id LIMIT 1", $quiz->id));
		 if(empty($question->id)) {
		 	 _e('This quiz has no questions.', 'chained');
		 	 return false;
		 }	
		 
		 // select possible answers
		 $choices = $wpdb->get_results($wpdb->prepare("SELECT * FROM ".CHAINED_CHOICES." 
		 	WHERE quiz_id=%d AND question_id=%d ORDER BY id", $quiz->id, $question->id));
		
		 // text captcha?
        if(!empty($quiz->require_text_captcha)) {	
            $text_captcha_html = ChainedTextCaptcha :: generate( $quiz_id );
            $textcaptca_style = $exam->single_page==1?"":"style='display:none;'";
            $text_captcha_html = "<div id='ChainedTextCaptcha' $textcaptca_style>".$text_captcha_html."</div>";	
        }
		 			 	
		 $first_load = true;			 	
		 $ui = get_option('chained_ui');
		 include(CHAINED_PATH."/views/display-quiz.html.php");
	}

Code file location:

chained-quiz/chained-quiz/models/basic.php

Conclusion

Now that you’ve learned how to embed the Chained Quiz Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

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