Print

Load Data into WordPress Media-Library

Load external media and display it local

The WordPress-Media-Library is the place where WordPress stores Mediafiles like images, pdf etc.. In a JSON-Datafeed are often URLs pointing to such mediafiles.

When displaying such mediafiles you can either load the files from the external URL or you store the file in WordPress Media-Libraray and use this file there for display. There is a JCI-twig-syntax for that!

twig: wp_mediastore

The PRO-JCI-Plugin has a extended twig-syntax. One of the added twig-functions is wp_mediastore, it’s used this way with the exmaple-JSON http://api.json-content-importer.com/extra/example-gen-cp-ts/example_events_venues.json

Create a JCI-Template with the example URL and this twig-code:

{% for item in _context %}
{% if item.venue.img %}   
    load {{item.venue.img}}<br>
    {% set mediafilename = wp_mediafilename(item.venue.img) %}
    mediafilename: {{mediafilename}}<br>
    {% set medialist = wp_get_cp_by_cpf_keyvalue("attachment", "jciloadedimage", mediafilename) %}
    {% if (medialist | length) > 0 %}
        {% set mediatitle = item.venue.name ~ ' UPDATE' %}
        {% set mediaslug = item.venue.name ~ ' UPDATE' %}
        {% set content =  item.venue.venue_extra_info ~ ' UPDATE' %}
        {% set parentPageId = "" %}
        {% set publishdate = "now"|date_modify("-40 day")|date("Y-m-d H:i:s") %}
        {% set postStatusUsed = "public" %} 
        {% set authorid = "" %} 
        {% set val = wp_update_custom_post(medialist.0, mediatitle, mediaslug, content, ' update excerpt', publishdate, postStatusUsed, authorid, debug) %}
        update {{medialist.0 }}<hr>
    {% else %} 
        {% set mediatitle = item.venue.name %}
        {% set mediaslug = item.venue.name %}
        {% set content = item.venue.venue_extra_info %}
        {% set parentPageId = "" %}
        {% set publishdate = "now"|date_modify("-4 day")|date("Y-m-d H:i:s") %}
        {% set postStatusUsed = "public" %} 
        {% set authorid = "" %} 
        {% set sourcetype = "" %} 
        {% set withpath = TRUE %} 
        {% set removeqm = FALSE %} 
        {% set generate_thumbnails = TRUE%} 
        {% set loadContext = "" %} 
        {% set addfileformatname = "" %} 
        {% set avariable = wp_mediastore(item.venue.img, parentPageId, mediatitle, mediaslug, content, "short text to image", publishdate, postStatusUsed, authorid, sourcetype, withpath, removeqm, generate_thumbnails, loadContext, addfileformatname) %}
        {{avariable}}<br>
        {% set avariableArr = avariable | json_decode(TRUE) %}
        {% set idofuploadedimage = avariableArr.jci.attachment_id %}
        {% set setcpf = wp_insert_custom_field_keyvalue(idofuploadedimage, "jciloadedimage", mediafilename) %}
        new uploaded image {{idofuploadedimage}}<hr>
    {% endif %}
{% endif %}
{% endfor %}

What is happening here:

  • By wp_mediafilename we get the name of the to be stored images
  • By wp_get_cp_by_cpf_keyvalue we check, if there is a attachement (which is quite similar to an ordinary page) with a special defined CustomPostField (CPF).
  • If there is such a CPF, there is already such an image with pageid returned by wp_get_cp_by_cpf_keyvalue. So we update this pageid.
  • If there is no such CPF, we do the upload by wp_mediastore. In return “wp_mediastore” sends an JSON-feed, which is used by the twig-function “json_decode(TRUE)” to create a twig-array you can use in the second line, e.g.:
{"jci":{"file":"http:\/\/api.json-content-importer.com\/extra\/example-gen-cp-ts\/images\/pal-museum.jpg","status":"ok","statusmsg":"ok","filename":"http-api.json-content-importer.com-extra-example-gen-cp-ts-images-pal-museum.jpg","attachment_id":82561,"attachment_url":"URL_OF_UPLOADED_IMAGE"}}

{% set idofuploadedimage = avariableArr.jci.attachment_id %}
gives the pageid of the new uploaded images. This pageid we can use to set the CPF by wp_insert_custom_field_keyvalue.

Was this article helpful?
0 out Of 5 Stars
5 Stars 0%
4 Stars 0%
3 Stars 0%
2 Stars 0%
1 Stars 0%
How can we improve this article?
Please submit the reason for your vote so that we can improve the article.
On this page: