Quiz Cat Shortcode

Below, you’ll find a detailed guide on how to add the Quiz Cat – WordPress 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 Quiz Cat – WordPress Quiz Plugin shortcode not to show or not to work correctly.

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

Plugin Icon
Quiz Cat – WordPress Quiz Plugin

"Quiz Cat – WordPress Quiz Plugin is your ideal tool for creating engaging quizzes. It's perfect for increasing user engagement, generating leads, or just adding fun interactive content to your WordPress site."

★★★★☆ (33) Active Installs: 5000+ Tested with: 6.3.2 PHP Version: false
Included Shortcodes:
  • [quiz-cat]

Quiz Cat [quiz-cat] Shortcode

The Quiz Cat shortcode is used to display a quiz on your WordPress site. It fetches the quiz data based on the ID specified in the shortcode attributes. The shortcode retrieves quiz details, questions, settings, and results. It also handles the quiz’s visual elements such as stylesheets and scripts. If no quiz is found with the given ID, it returns a message to the user.

Shortcode: [quiz-cat]

Parameters

Here is a list of all possible quiz-cat shortcode parameters and attributes:

  • id – Unique identifier for the specific quiz

Examples and Usage

Basic Example – Display a quiz by referencing its ID

[quiz-cat id=1 /]

Advanced Examples

Displaying a quiz by referencing both ID and title. The quiz will first try to load by ID, but if not found, it will try to load by title.

[quiz-cat id=1 title="PHP Quiz" /]

Displaying a quiz with additional parameters. Here, we’re specifying the ID, title, and also setting the ‘autostart’ parameter to true, which means the quiz will automatically start when the page loads.

[quiz-cat id=1 title="PHP Quiz" autostart=true /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'quiz-cat', 'fca_qc_do_quiz' );

Shortcode PHP function:

function fca_qc_do_quiz( $atts ) {

	if ( !empty ( $atts[ 'id' ] ) ) {

		$post_id = intVal ( $atts[ 'id' ] );
		$all_meta =  get_post_meta ( $post_id, '', true );
		$quiz_meta = empty ( $all_meta['quiz_cat_meta'] ) ? array() : unserialize( $all_meta['quiz_cat_meta'][0] );
		
		$quiz_meta['title'] = get_the_title ( $post_id );
		$questions = empty ( $all_meta['quiz_cat_questions'] ) ? array() : unserialize( $all_meta['quiz_cat_questions'][0] );
		$quiz_settings = empty ( $all_meta['quiz_cat_settings'] ) ? array() : unserialize( $all_meta['quiz_cat_settings'][0] );
		$restart_button = empty ( $quiz_settings['restart_button'] ) ? false : true;
		$optin_settings = empty ( $all_meta['quiz_cat_optins'] ) ? array() : unserialize( $all_meta['quiz_cat_optins'][0] );
		$timer_mode = empty( $quiz_settings['timer_mode'] ) ? 'off' : $quiz_settings['timer_mode'];
		$draw_optins = empty( $optin_settings['capture_emails'] ) ? false : true;
		$quiz_results = empty ( $all_meta['quiz_cat_results'] ) ? array() : unserialize( $all_meta['quiz_cat_results'][0] );
		$autostart_quiz = empty ( $quiz_settings['autostart_quiz'] ) ? false : true;
		
		foreach( $quiz_results as $key => $value ){
			$quiz_results[$key]['desc'] = do_shortcode( $value['desc'] );
		}

		if ( !$quiz_meta || !$questions ) {
			return '<p>Quiz Cat: ' . esc_attr__('No Quiz found', 'quiz-cat') . '</p>';
		}
		
		wp_enqueue_script( 'jquery' );
		wp_enqueue_style( 'fca_qc_quiz_stylesheet', FCA_QC_PLUGINS_URL . '/includes/quiz/quiz.min.css', array(), FCA_QC_PLUGIN_VER );
		wp_enqueue_script( 'fca_qc_img_loaded', FCA_QC_PLUGINS_URL . '/includes/quiz/jquery.waitforimages.min.js', array(), FCA_QC_PLUGIN_VER, true );
		
		if ( $draw_optins ) {
			wp_enqueue_style( 'fca_qc_tooltipster_stylesheet', FCA_QC_PLUGINS_URL . '/includes/tooltipster/tooltipster.bundle.min.css', array(), FCA_QC_PLUGIN_VER );
			wp_enqueue_style( 'fca_qc_tooltipster_borderless_css', FCA_QC_PLUGINS_URL . '/includes/tooltipster/tooltipster-borderless.min.css', array(), FCA_QC_PLUGIN_VER );
			wp_enqueue_script( 'fca_qc_tooltipster_js', FCA_QC_PLUGINS_URL . '/includes/tooltipster/tooltipster.bundle.min.js', array('jquery'), FCA_QC_PLUGIN_VER, true );
			wp_enqueue_script( 'fca_qc_jstz_js', FCA_QC_PLUGINS_URL . '/includes/quiz/jstz.min.js', array(), FCA_QC_PLUGIN_VER, true );
		}
		
		if ( FCA_QC_DEBUG ) {
			wp_enqueue_script( 'fca_qc_quiz_js', FCA_QC_PLUGINS_URL . '/includes/quiz/quiz.js', array( 'jquery', 'fca_qc_img_loaded' ), FCA_QC_PLUGIN_VER, true );
		} else {
			wp_enqueue_script( 'fca_qc_quiz_js', FCA_QC_PLUGINS_URL . '/includes/quiz/quiz.min.js', array( 'jquery', 'fca_qc_img_loaded' ), FCA_QC_PLUGIN_VER, true );
		}
		
		//DONT SEND API KEYS TO CLIENT SIDE JS
		$unset_options = array(
			'drip_key',
			'drip_id',
			'drip_tags',
			'activecampaign_key',
			'activecampaign_url',
			'activecampaign_tags',
			'getresponse_key',
			'api_key',
			'mailchimp_groups',
			'aweber_key',
			'aweber_tags',
			'madmimi_key',	
			'madmimi_email',	
			'campaignmonitor_key',
			'campaignmonitor_id',	
			'convertkit_key',
			'convertkit_tags',
			'convertkit_key',
			'zapier_url',
		);
			
		forEach ( $unset_options as $o ) {
			if ( isSet( $optin_settings[$o] ) ) { 
				unset( $optin_settings[$o] );
			}
		}		
		
		$quiz_text_strings = fca_qc_set_quiz_text_strings( $post_id );
		global $global_quiz_text_strings;
					
		$quiz_data = array(
			'quiz_id' => $post_id,
			'quiz_meta' => $quiz_meta,
			'questions' => $questions,
			'quiz_results' => $quiz_results,
			'quiz_settings' => $quiz_settings,
			'time_taken_string' => empty( $quiz_text_strings[ 'time_taken' ] ) ? $global_quiz_text_strings[ 'time_taken' ] : $quiz_text_strings[ 'time_taken' ],
			'timeout_string' => empty( $quiz_text_strings[ 'timedout' ] ) ? $global_quiz_text_strings[ 'timedout' ] : $quiz_text_strings[ 'timedout' ],
			'wrong_string' => $quiz_text_strings[ 'wrong' ],
			'correct_string' => $quiz_text_strings[ 'correct' ],
			'your_answer_string' => $quiz_text_strings[ 'your_answer' ],
			'correct_answer_string' => $quiz_text_strings[ 'correct_answer' ],
			'optin_settings' => $optin_settings,
			'nonce' => wp_create_nonce('fca_qc_quiz_ajax_nonce'),
			'ajaxurl' => admin_url('admin-ajax.php'),
			'default_img' => FCA_QC_PLUGINS_URL . '/assets/quizcat-240x240.png',
			'gdpr_checkbox' => fca_qc_show_gdpr_checkbox(),
		);
		if ( is_user_logged_in() ) {
			$user = wp_get_current_user();
			if ( $user->ID !== 0 ) {
				$quiz_data['user'] = array (
					'name' => $user->user_firstname,
					'email' => $user->user_email,
					
				);
			}
		}
		wp_localize_script( 'fca_qc_quiz_js', "quizData_$post_id", $quiz_data );
		wp_localize_script( 'fca_qc_quiz_js', "fcaQcData", array( 
			'debug' => FCA_QC_DEBUG,
			'analytics' => !defined( 'fca_qc_disable_activity' ) && function_exists('fca_qc_add_activity')
		) );

		
		//ADD IMPRESSION
		if ( function_exists('fca_qc_add_activity') ) {
			$return = fca_qc_add_activity( $post_id, 'impressions' );
		}
		
		$title = empty( $quiz_meta['title'] ) ? '' : $quiz_meta['title'];
		$desc = empty( $quiz_meta['desc'] ) ? '' : $quiz_meta['desc'];
		$desc_img_src = empty( $quiz_meta['desc_img_src'] ) ? '' : $quiz_meta['desc_img_src'];
		
		ob_start(); ?>
		
		<?php echo fca_qc_maybe_add_custom_styles( $post_id ) ?>
		
		<div class='fca_qc_quiz' id='<?php echo "fca_qc_quiz_$post_id" ?>'>
			<span class='fca_qc_mobile_check'></span>
			<?php if ( $autostart_quiz === false ) { ?>
			<p class='fca_qc_quiz_title'><?php echo fca_qc_kses_html( $title ) ?></p>
			<div class='fca_qc_quiz_description'><?php echo fca_qc_kses_html( do_shortcode( $desc ) )?></div>
			<img class='fca_qc_quiz_description_img' src='<?php echo esc_attr( $desc_img_src ) ?>'>
			<button type='button' class='fca_qc_button fca_qc_start_button'><?php echo fca_qc_kses_html( $quiz_text_strings[ 'start_quiz' ] ) ?></button>
			<?php } ?>
			
			<div class='flip-container fca_qc_quiz_div' style='display: none;'>

				<?php if( $timer_mode !== 'off' ){ ?>
					<div class='fca_qc_timer'>
						<span class='fca_qc_timer_hours'></span>
						<span class='fca_qc_timer_minutes'></span>
						<span class='fca_qc_timer_seconds'></span>
					</div>
				<?php } ?>
				<div class='fca-qc-flipper'>
					<?php echo fca_qc_do_question_panel( $post_id, $quiz_text_strings ) ?> 
					<?php echo fca_qc_do_answer_panel( $quiz_text_strings, $post_id ) ?> 
					
				</div>
			</div>
			<?php echo fca_qc_do_score_panel( $post_id, $quiz_text_strings ) ?> 
			
			<div class='fca_qc_quiz_footer' style='display: none;'>
				<span class='fca_qc_question_count'></span>		
			</div>
			<?php if ( $draw_optins && function_exists('fca_qc_do_optin_panel') ) {
					echo fca_qc_do_optin_panel( $optin_settings, $quiz_text_strings );
				}?>
			
			<?php echo fca_qc_do_your_answers_panel( $quiz_text_strings ) ?> 
			
			<?php if ( $restart_button ) {
				$button_text = fca_qc_kses_html( $quiz_text_strings[ 'retake_quiz' ] );
				echo "<button type='button' class='fca_qc_button' id='fca_qc_restart_button' style='display: none;'>$button_text</button>";
				
			}?>

			
		</div>
		<?php
		
		return ob_get_clean();
	} else {
		return '<p>Quiz Cat: ' . esc_attr__('No Quiz found', 'quiz-cat') . '</p>';
	}
}

Code file location:

quiz-cat/quiz-cat/includes/quiz/quiz.php

Conclusion

Now that you’ve learned how to embed the Quiz Cat – WordPress 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 *