Want more features on Pastebin? Sign Up, it's FREE!
Guest

How to Pass External Variables to Filters/Actions

By: a guest on Apr 11th, 2012  |  syntax: PHP  |  size: 1.80 KB  |  views: 158  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. $score = 0;
  3. $questions = get_quiz_result_questions();
  4. $total_questions = 0;
  5. foreach( $questions as $question ) {
  6.         $order = $question->order;
  7.        
  8.         if( $order >= 100 ) {
  9.                 break;
  10.         }
  11.        
  12.         if( $question->correct == $_POST['Q'][$order] ) {
  13.                 $score++;
  14.         }
  15.         $total_questions++;
  16. }
  17.  
  18. global $wp;
  19.  
  20. global $score;
  21. $wp->vars_for_filter = array($score, $total_questions);
  22.  
  23. function add_score_to_title($title) {
  24.         global $wp;
  25.         $score = $wp->vars_for_filter[0];
  26.         $total_questions = $wp->vars_for_filter[1];
  27.         return 'Quiz Results (' . $score . '/' . $total_questions . ') - ' . $title;
  28. }
  29.  
  30. add_filter( 'aioseop_title_single', 'add_score_to_title');
  31. get_header();
  32. ?>
  33. <div id="content" class="quiz-results wide">
  34. <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  35.         <h1><?php the_title(); ?> Results</h1>
  36.     <p>Score: <?=$score?> out of <?=$total_questions;?></p>
  37.         <table>
  38.     <thead>
  39.         <tr>
  40.                 <th class="question">Question</th>
  41.             <th class="your-answer">You Said</th>
  42.             <th class="correct-answer">Correct Answer</th>
  43.         </tr>
  44.     </thead>
  45.     <tbody>
  46. <?php
  47.     foreach( $questions as $question ) {
  48.         $order = $question->order;
  49.        
  50.         if( $order >= 100 ) {
  51.                 break;
  52.         }
  53.        
  54.         if( $question->correct == $_POST['Q'][$order] ) {
  55.                 $class='correct';
  56.         } else {
  57.                 $class='wrong';
  58.         }
  59.        
  60.         $your_answer = $question->answers[$_POST['Q'][$order] - 1];
  61.         $correct_answer = $question->answers[$question->correct - 1];  
  62.         ?>
  63.     <tr>
  64.         <td class="question"><strong><?=$order;?></strong> <?=$question->question;?></td>
  65.         <td class="your-answer <?=$class;?>"><?=$your_answer;?></td>
  66.         <td class="correct-answer"><?=$correct_answer;?></td>
  67.     </tr>
  68.     <?php
  69. }
  70. ?>
  71.          </tbody>
  72.    
  73.     </table>
  74. <?php endwhile; endif; ?>
  75. </div>
  76. <?php get_footer(); ?>
clone this paste RAW Paste Data