Last active
September 6, 2017 09:09
-
-
Save MaximeCulea/6b02e287d9d7867c5fe15fd512c7d21d to your computer and use it in GitHub Desktop.
WP Media API - not in collection case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Here the element is an input with the media ID that we hide to keep the value for POST | |
* But we still want an other display, so add html after | |
* @author Maxime CULEA | |
*/ | |
function transform_qrcode_p2p_meta(element) { | |
element.hide(); | |
/** | |
* Use WP Media JavaScript API | |
* As stipulated, the value from element is a media ID | |
* So we use it into the WP Media API to fetch the media element to add it to the media collection | |
* And also to work directly with | |
*/ | |
wp.media.attachment(element.attr('value')).fetch().done(function () { | |
// Work on media attributes | |
media = this.attributes; | |
/** | |
* Html to force download. In a href the download attr is enough to fire force download. | |
* | |
* @see : https://stackoverflow.com/questions/5192917/force-download-through-js-or-query/18330145#18330145 | |
* @see : https://www.w3schools.com/TAgs/att_a_download.asp | |
* | |
* @var string href : the url to the media | |
* @var string download : the media title on download (no need extension) | |
*/ | |
element.after('<a href="' + media.url + '" download="' + media.title + '">' + media.id + '</a>'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment