List quizzes

closed

Hi, I created a small snippet on wordpress to massively update quiz settings. However, I can’t seem to set some values to 1 or “yes”. Can you tell me what I’m doing wrong? Thank you.

function list_quizzes()
{
    $pass_text = '[right_answer]';
    $fail_text = '[wrong_answer]';

    $taxonomy = 'quiz';
    $term_args = array(
        'hide_empty' => false,
    );
    $tax_terms = get_terms($taxonomy, $term_args);

    if (!empty($tax_terms) && !is_wp_error($tax_terms)) {
        foreach ($tax_terms as $quiz) {
            $quiz_id = $quiz->term_id;
            $fields = get_hdq_quiz($quiz_id);
            $fields["quiz_pass_text"]["value"] = $pass_text;
            $fields["quiz_fail_text"]["value"] = $fail_text;
            $fields["quiz_pass_percentage"]["value"] = 100;
            $fields["hide_questions"]["value"] = "true";
            $fields["share_results"]["value"] = 0;
            $fields["results_position"]["value"] = "below";
            $fields["show_results_now"]["value"] = "true";
            $fields["stop_answer_reselect"]["value"] = "true";
            update_term_meta($quiz_id, "quiz_data", $fields);
        }
    }
}
list_quizzes();
September 8, 2023 at 1:46pmBeniamino

I think there are just two things to note with your code.

Example: $fields["hide_questions"]["value"] = "true";

What I think you are trying to do is set that value to true – to use it as a boolean.

First, since you have it wrapped in quotes, you are setting it as a string, not a boolean.

Second, these fields are not supposed to be bolleans, they need to be arrays. If you were to print_r $fields, you’ll see how the data needs to be stored. To use “hide_questions” as the example again, what you’d need is:

$fields["hide_questions"]["value"] = array("yes");

FYI: The difference for fields that need to be arrays such as “hide_questions” and ones that don’t such as “results_position” is how HD Quiz looks at these fields. As either radio inputs (can only have a single value), or as a checkbox input (can have multiple values, hence the array).

Hope this helps!

08 September 2023 — 11:45support admin Dylan

This thread has either been marked as complete or has been automatically closed due to inactivity. Please consider opening a new support thread for help.