[WPMU DEV Dashboard] After submission of quiz is there a way we can redirect the URL to a dedicate

After submission of quiz is there a way we can redirect the URL to a dedicated answers page? I know the functionality exists for the forms, so after you submit a form you can choose to redirect to a separate page. We need to redirect the quiz AFTER answering to an answer page with the results of the quiz on that page. We are using daily quizzes so we can't base anything off of a quiz ID because the ID will change everyday. Thank you so much!

  • Tho Bui
    • Staff

    Hello Aly,

    I hope you're well today!

    Regarding your question, I have prepared a custom snippet which you can use as a guide here :
    https://gist.github.com/wpmudev-sls/4ca2713094916e4cd29242d36c486efc

    You can download a zip, unzip it and upload file forminator-custom-event-js-on-submit-success.php to your wp-content/mu-plugins folder. If that folder doesn't exist you can simply create it.

    After you activating this, you can add this custom function on js file to trigger event submit success:

    window.wpmudev_forminator_event_on_submit_quiz_success = function( e, formData, _form ){
      var regexp = RegExp( '\\%5B(question[-\\d]+)\\%5D\\=(\\d)', 'g' );
      var _settings = _form.data('settings'),
        _parse_data;
      var _redirect_url = 'YOUR-URL-REDIRECT-HERE';
    
      if( -1 === _redirect_url.indexOf('?') ){
        _redirect_url += '?';
      }else{
        _redirect_url += '&';
      }
      _redirect_url += 'form-id='+ _settings.form_id; //add form id
    
      if( 'after' === _settings.results_behav ){
        _parse_data = regexp.exec( formData );
        if( _parse_data ){
          _redirect_url += '&question='+ _parse_data[1]; //question
          _redirect_url += '&answer=' + _parse_data[2]; //answer index
    
          // additional info - optional
          var _question = jQuery('#'+ _parse_data[1] );
          if( _question.length ){
            var _el_answer = _question.find('.forminator-answer').eq(_parse_data[2])
            _redirect_url += '&answer-text=' + _el_answer.find('.forminator-answer--name').text(); //answer text
            _redirect_url += '&is-correct=' + _el_answer.hasClass('forminator-is_correct'); //answer status
          }
    
          window.location.href = _redirect_url;
        }
      }else{
        var _data = {
          questions: [],
          answers: []
        };
        while ( (_parse_data = regexp.exec(formData)) !== null ) {
          _data['questions'].push( _parse_data[1] ); //list questions
          _data['answers'].push( _parse_data[2] ); //list answers index
        }
        if( _data['questions'].length ){
          _redirect_url += '&questions='+ _data['questions'].join(',');
          _redirect_url += '&answers=' + _data['answers'].join(',');
    
          window.location.href = _redirect_url;
        }
      }
    }

    Please replace YOUR-URL-REDIRECT-HERE with your page url redirect .

    And in your destination page, you can try this PHP custom snippet code to retrieve the data:

    <?php
      if( isset( $_GET['form-id'] ) && class_exist('Forminator_Quiz_Form_Model') ){
        $model = Forminator_Quiz_Form_Model::model()->load( $_GET['form-id'] );
        if( ! empty( $model ) ){
          $model_questions = $model->questions;
    
          if( ! empty( $_GET['question'] ) ){ //single question
            $question = $_GET['question'];
            $answer_index = $_GET['answer'];
            if( $_GET['answer-text'] ){
              $answer_text = $_GET['answer-text'];
            }else{
              $answer_text = null;
              foreach( $model_questions as $key => $qob ){
                if( $question !== $qob['slug'] ) continue;
                $answer_text = $qob['answers'][ $answer_index ]['title'];
                break;
              }
            }
    
            // var_dump( $question, $answer_index, $answer_text );
          }elseif( ! empty( $_GET['questions'] ) ){ //multiple questions
            $questions = explode(',', $_GET['questions'] );
            $answers = explode(',', $_GET['answers'] );
            foreach( $questions as $k => $question ){
              $answer_index = $answers[ $k ];
              if( ! isset( $model_questions[ $k ] ) || $model_questions[ $k ]['slug'] !== $question ){
                if( ! isset( $model_questions[ $question ] ) ){
                  foreach( $model_questions as $key => $qob ){
                    unset( $model_questions[ $key ] );
                    $model_questions[ $qob['slug'] ] = $qob;
                  }
    
                  if( ! isset( $model_questions[ $question ] ) ){
                    continue;
                  }
                }
                $k = $question;
              }
              $answer_text = $model_questions[ $k ]['answers'][ $answer_index ]['title'];
    
              // var_dump( $question, $answer_index, $answer_text );
            }
          }
        }
      }

    If you are not familiar with mu-plugins you can read about them here:
    https://premium.wpmudev.org/manuals/wpmu-manual-2/using-mu-plugins/
    or you can paste it in your child theme's functions.php file.

    Hope this helps!

    Kind regards,
    Tho Bui

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.