
How to Pass External Variables to Filters/Actions
By: a guest on
Apr 11th, 2012 | syntax:
PHP | size: 1.80 KB | hits: 88 | expires: Never
<?php
$score = 0;
$questions = get_quiz_result_questions();
$total_questions = 0;
foreach( $questions as $question ) {
$order = $question->order;
if( $order >= 100 ) {
break;
}
if( $question->correct == $_POST['Q'][$order] ) {
$score++;
}
$total_questions++;
}
global $wp;
global $score;
$wp->vars_for_filter = array($score, $total_questions);
function add_score_to_title($title) {
global $wp;
$score = $wp->vars_for_filter[0];
$total_questions = $wp->vars_for_filter[1];
return 'Quiz Results (' . $score . '/' . $total_questions . ') - ' . $title;
}
add_filter( 'aioseop_title_single', 'add_score_to_title');
get_header();
?>
<div id="content" class="quiz-results wide">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?> Results</h1>
<p>Score: <?=$score?> out of <?=$total_questions;?></p>
<table>
<thead>
<tr>
<th class="question">Question</th>
<th class="your-answer">You Said</th>
<th class="correct-answer">Correct Answer</th>
</tr>
</thead>
<tbody>
<?php
foreach( $questions as $question ) {
$order = $question->order;
if( $order >= 100 ) {
break;
}
if( $question->correct == $_POST['Q'][$order] ) {
$class='correct';
} else {
$class='wrong';
}
$your_answer = $question->answers[$_POST['Q'][$order] - 1];
$correct_answer = $question->answers[$question->correct - 1];
?>
<tr>
<td class="question"><strong><?=$order;?></strong> <?=$question->question;?></td>
<td class="your-answer <?=$class;?>"><?=$your_answer;?></td>
<td class="correct-answer"><?=$correct_answer;?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php endwhile; endif; ?>
</div>
<?php get_footer(); ?>