Support » Developing with WordPress » How to store question data for quiz app in wp

  • Hello,

    How would I be able to store questions for use in my quiz app?

    I would like to store questions as posts using markup like this, however, the editor inserts line breaks and paragraphs which isn’t very use friendly.

    What is the best way to store data like this so I can use the json for my app?

    <question>
    What event marked the start of World War II?
    </question>
    <correct_answer>
    Invasion of Poland (1939)
    </correct_answer>
    <incorrect_answers>
    ["Invasion of Russia (1942)","Battle of Britain (1940)","Invasion of Normandy (1944)"]
    </incorrect_answers>

    Displays as this in json:

    content":{"rendered":"<p><question><br \/>\nWhat event marked the start of World War II?<br \/>\n<\/question><br \/>\n<correct_answer><br \/>\nInvasion of Poland (1939)<br \/>\n<\/correct_answer><br \/>\n<incorrect_answers><br \/>\n[“Invasion of Russia (1942)”,”Battle of Britain (1940)”,”Invasion of Normandy (1944)”]<br \/>\n<\/incorrect_answers><br \/>\n<question><br \/>\ntesting<br \/>\n<\/question><br \/>\n<correct_answer><br \/>\nyseesf<br \/>\n<\/correct_answer><br \/>\n<incorrect_answers><br \/>\n[“gffdg”,”fdgfdg”,”dfgfdgfd”]<br \/>\n<\/incorrect_answers><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What event marked the start of World War II? Invasion of Poland (1939) [“Invasion of Russia (1942)”,”Battle of Britain (1940)”,”Invasion of Normandy (1944)”] testing yseesf [“gffdg”,”fdgfdg”,”dfgfdgfd”]<\/p>\n

    • This topic was modified 1 month, 2 weeks ago by  matthisco.
    • This topic was modified 1 month, 2 weeks ago by  matthisco.
    • This topic was modified 1 month, 2 weeks ago by  matthisco.
    • This topic was modified 1 month, 2 weeks ago by  matthisco.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Unless your site has no other need for blog posts now or well into the future, you should consider a custom post type to contain quiz data. The editor will continue to be an annoyance with custom post types. You could try to disable the functions that add undesirable behavior. It might be easier to do so with the classic editor. A couple offenders are wptexturize and wpautop. Search the global $wp_filters array to determine which filters these are hooked to.

    Another option would be to strip out undesirable elements added by the editor before you use the data. A third option available to custom post types is to not enable the editor for the post type and instead add a metabox with a plain textarea to serve as the “editor”.

    Thanks for the reply.

    Could I not just add questions and an answer as a custom field for each post?

    Questions could be a string, answers could be a comma separated list and the correct answer could be an int as the index for the answers.

    Moderator bcworkz

    (@bcworkz)

    Custom fields are an option as well. I was focused on using post content to contain your JSON data, but post meta is fair game. Meta queries are rather less efficient than content, but unless you anticipate many thousands of questions, I don’t think there will be an issue. Simply storing data in meta will likely make for a better user experience in creating new questions.

    Once you prevent WP from corrupting your data, you are free to use any string based storage format you wish: plain text, JSON, CSV, etc., even serialized arrays if you want. Keep in mind what SQL must do to locate the proper records. base64 encoding might not be a good choice 😉 Keep it simple.

    Thanks again for the reply. I have added all my custom fields. Is there a way I can store them in an array of objects called questions? There will be 15 questions in total.

    I need each questionlike this:

    question = [{
       question: ''
       answers: ''
       correctAnswer: ''
    }]

    Here is my functions.php:

    	// question1
      register_rest_field('post', 'question1', 
        array(
        'get_callback' => 'post_get_meta_cb', 
        'update_callback' => 'post_update_meta_cb', 
        'schema' => null
        )
      );
    	
    	  register_rest_field('post', 'answers1', 
        array(
        'get_callback' => 'post_get_meta_cb', 
        'update_callback' => 'post_update_meta_cb', 
        'schema' => null
        )
      );
    	
    	  register_rest_field('post', 'correctAnswer1', 
        array(
        'get_callback' => 'post_get_meta_cb', 
        'update_callback' => 'post_update_meta_cb', 
        'schema' => null
        )
      );
    	
    		// question2
      register_rest_field('post', 'question2', 
        array(
        'get_callback' => 'post_get_meta_cb', 
        'update_callback' => 'post_update_meta_cb', 
        'schema' => null
        )
      );
    	
    	  register_rest_field('post', 'answers2', 
        array(
        'get_callback' => 'post_get_meta_cb', 
        'update_callback' => 'post_update_meta_cb', 
        'schema' => null
        )
      );
    	
    	  register_rest_field('post', 'correctAnswer2', 
        array(
        'get_callback' => 'post_get_meta_cb', 
        'update_callback' => 'post_update_meta_cb', 
        'schema' => null
        )
      );
    • This reply was modified 1 month, 1 week ago by  matthisco.
    Moderator bcworkz

    (@bcworkz)

    Technically speaking, no, you must store strings. Practically speaking, yes, because arrays can be converted back and forth to strings. Javascript arrays can be converted to JSON format, a string representation of arrays. PHP arrays can be serialized and unserialized. Serialized arrays are string representations of arrays. WP will automatically serialize any arrays you attempt to save to meta data and unserialize upon retrieval.

    In JavaScript, use JSON methods to parse or stringify array data. PHP can also JSON encode or decode PHP array data if you like.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.