• Resolved Harry Wood

    (@harry-wood)


    I was looking to code my own stand-alone export tool. I got as far as figuring out where the quiz questions & answers data can be found within wp_postmeta with meta_key=’question_data’, but the actual data value is very cryptic-looking not-quite-JSON. A jumble of fields with what looks like integer/string types and field sizes.

    a:11:{s:11:"question_id";a:5:{s:4:"name";s:11:"question_id";s:4:"type";s:7:"integer";s:8:"required" ... etc

    Is that format designed by you for this quiz plugin? Is it documented anywhere? or maybe it’s a wordpress thing I should know about (I’m not really a wordpress developer so apologies if this is a stupid question)

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Harmonic Design

    (@harmonic_design)

    What you are seeing is a PHP/SQL thing – essentially a serialized array.

    You should absolutely NOT be working with the data raw from the database though.
    Take a look at ./hd-quiz/includes/tools/csv_import.php starting ~line 200 to see an example of how data should be sanitized and parsed.

    Here is some sudo code to help get you on the right track.

    Create a WP_QUERY to loop through the questions (post_type_questionna)

    Inside that loop, grab the question data. Something like

    $data = get_post_meta($question_id, "question_data", true);

    $data is now an associative array that contains all of the question data. But there is no guarantee that this data is safe! So you need to loop through the data and sanitize each field for security. Each field in the array contains two pieces of information: the field type, and the field name. Type indicates… the type of data it is and will help you sanitize accordingly. For example, a type of integer should be sanitized using intval, and a type of text should use sanitize_text_field. You can the idea.

    This should be more than enough to steer you in the right direction, so good luck!

    Thread Starter Harry Wood

    (@harry-wood)

    Thanks very much for those pointers. PHP serialization! Just as I half-suspected …it was a stupid question 🙂

    I have been able to create an exporter script which unserializes using get_post_meta as you suggested. Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Exporting to CSV’ is closed to new replies.