Search code examples
jsonwordpresstemplate-engine

recommended architecture for a wordpress data-centric site ? (data coming from JSON feed)


I am new, to wordpress, php, and the concept of parsing a Json data file and displaying it as the content of my page. I am working with wordpress, and I have tried a plugin JSON Content Importer.. but I cannot do a loop in the free version. So please, if you could (maybe), that it could be also very easy, to use a simple PHP lib (which one ?) to parse the Json file, and that the binding with the view should be done like that (I do not know if I should use a template library (moustache ? twig ?) to map my Json fields, with the view ?).

So maybe it is not dificult, but I do not find the 'simpliest' way to acheive this. A similar github project would also be perfect to inspire me. Thanks you


Solution

  • To answer on your comment above - depends on what you want to do with the json.

    If you're using it to input some kind of data that will be used in the options, you can put it in the functions.php. If you want to display it, you can put it in a template file, and then create a page that uses that template.

    If you're fetching it from a url, you should use 'wp_remote_get()'. Kinda like:

    $request = wp_remote_get($url);
    $response = wp_remote_retrieve_body( $request );
    // You can check the response before outputing it.
    $output = json_decode( $response );
    

    $outputcontains an array of your data. With it, you can use it in whatever way you want - loop through it, or use parts of it...