PRO-Version: Create Pages

Usually the JCI-PLugin retrieves JSON from an API, converts the data with a template to HTML and displays this HTML. That’s it.
From PRO-Version 3.4.0 on the JCI-Plugin also can create WordPress-Custom Posts (aka WordPress-pages) out of JSON.
This is how it works:

Generate CPT: See Video and code-examples here

  • Step 1:
    Define / register a “Custom Post Type” in the Options of the JCI-plugin, there the Tab “Custom Post Types”.
    Here you have to define three parameters importains for the Custom PostType:
    – type: Singluar name of the Custom Post Type. The Plugin adds automatic “jci_” as prefix to avoid trouble with other plugins
    – ptredirect: Path for the URL of the created Custom Post
    – ptname: Menuname in the WordPress-Dashboard
    – key (new in version 3.4.9): Unique, random string to connect all pages created by this JSON (when deleting, all pages with that key are deleted)
    The keys and values have to be separated by “=” and the pairs by “;”. If you want to define more than one Custom Post Type separate by “##”. Example:

    new in Version 3.4.13: When setting “deleteold=some” you can use twig-code in “key”. E. g. key=date{{“now”|date(“Ymd”)}} creates keys with the current date.
    If you enhance the twig-code for the key by if-then-else and JSON-data you can build keys for each JSON-item. If the key is empty nothing is deleted or created. When updating the CPT with new JSON you can delete some old CPT and keep some CPT.

    The WordPress-Admin-Menu should have the items “MyCreatedPosts” and “MyCreatedPost1”.

  • Step 2:
    Set Permalink “Post name”
    Click to the general Settings of WordPress, there “Permalinks” and select “Post name”-Radiobutton.
  • Step 3:
    Create JCI-template
    Click to the Plugins template-manager and insert some dummy text. Save it. Then browse the list of templates and get the id of this template, let’s call it TEMPLATE_ID_FOR_CUSTOM_POST.
    This template is used for building each single Custom Post (we enhance it later).
  • Step 4:
    Create a new WordPress Custom Page:
    There we place all we need to create Custom Posts like the shortcode (we do create one in the next step).
    Required for Plugins prior to 3.4.9 or not using the “key=” as described in Step 1:
    Create a new “Custom Post Field”, named “jci_uniquekey_createpost” with a random, unique number (at least 10 characters, you’re free to create one). By this unique number all created Custom Posts can be found. Title of this page can be something like “Create Custom Post”.
  • Step 5:
    Set up a Shortcode in a new WordPress Page:
    [jsoncontentimporterpro url=URL_WITH_DATA_FOR_ONE_OR_MORE_CUSTOM_POST parser=twig debugmode=10 id=TEMPLATE_ID_FOR_CUSTOM_POST]
    In detail:
    This is the URL with all the data need for one or more Custom Posts
    – id=TEMPLATE_ID_FOR_CUSTOM_POST: see Step 4
    – Additional Parameter: Up to now we’re creating a usual get-and-display-page. So if you need more parameter just add it. E.g. method=post, payload?=etc.
  • Step 6:
    View Creating Page
    Preview the so far Page with will later create the Custom Posts. You shoud see some debug-messages: Esp. the JSON-Code retrieved should be there. If not you have to check the Shortcode for missing parameter. Also the template-dummytext should be there (see Step 4).
    If this looks good publish this page.
  • Step 7:
    Check the JSON-data
    You should be familiar with the JSON you retrieve. You can copy paste the JSON into to see the structure. As you want to create a set of Custom Posts you need a set of data for that. You must identify the path to such a set in your JSON.
  • Step 8:
    Enhance Shortcode:
    Add mode and createoptions to your Shortcode (the one created in Step 5)
    [jsoncontentimporterpro url=URL_WITH_DATA_FOR_ONE_OR_MORE_CUSTOM_POST parser=twig id=TEMPLATE_ID_FOR_CUSTOM_POST mode=create createoptions='{“type”:”mynewpagetype“, “loop”:”data”, “title”:”this is the title {{title}}: {{headline}}”, “slugname”: “{{title|replace({#SQM#/#SQM#: #SQM#-#SQM#})}}-{{headline | lower | replace({#SQM# #SQM#: #SQM#-#SQM#}) }}”, “postdateoffset”:”VALUE”, “deleteold”:”yes”}’]
    In detail:
    – mode=create: This sets an plugin-call into “create Custom Posts mode”
    – createoptions=…: JSON-coded parameter on how to build the Custom Posts.
    — “type”:”mynewpagetype“: This is the link to the Custom Post Type defined in Step 1
    — “loop”:”data”: The Plugin needs a set of data (aka array) in the JSON-data. This is defined here. See Step 7 for that.
    — “title”:”this is the title”: twig-template for building the Title of the created Custom Post. E.g. find in your JSON data for the Custom Post HTML-title. E.g. “{{title}}”.
    — “slugname”: “thisistheurl-add-some-individual-data”: twig-template for building the slug/URL of the created Custom Post. Finde something in the JSON-data for that, like “{{id}}”.
    — “postdateoffset”: Date of generated Custom Post Types:
    set “VALUE” for the time and date of the generated Custom Post Fields.
    If not set the current PHP-Servertime is used.
    “VALUE” can be numeric: Then this number is subtracted from the current Servertime, e.g. one hour is 3600 seconds or 3600000 miliseconds.
    “VALUE” can be “wptimezone”: Then the in the WordPress-installation defined timezone is used
    “VALUE” can be a valid timezonestring
    — “deleteold”:”yes”: When creating new Custom Posts all previous Custom Posts are deleted (they are identified by “jci_uniquekey_createpost”, see Step 2)
  • Step 9:
    Create Custom Posts
    Publish the creating Custom Posts with the enhanced Shortcode and call it (you can switch off the template by adding “?show=oc”): This should create Custom Posts and show some debug-messages.
    The content of the created Custom Post should be the template-dummytext.
  • Step 10:
    Enhance the template
    Click to the Plugins template-manager and open the template with the dummytext. Here you can build the content which is put into the created Custom Posts. This depends on the JSON-data you have. If you need to call the API again for retrieving more, Custom Post-specific data you can do that by adding another [[jsoncontentimporterpro…]]-Shortcode parametrized by the data from the initial JSON-API-call.
  • Step 11:
    Create Pages
    Publish creating page and view it in the browser. You should see some debug-messages and the status of the creation-process.
  • Step 12:
    In case of success in the WordPress-Menu a entry “ptname” (see Step 1) is created with a number of pages. Thes pages should be publshed pages with a URL “ptredirect”  (see Step 1) plus “slugname” (see Step 3)