Closed mnguye08 closed 8 years ago
Hi @mnguye08, what's the URL? Thanks
Hi @airesvsg, for example, with this tweet https://twitter.com/lequipe/status/734278660237070336?lang=fr, the code generated by the oEmbed field is
<blockquote class=\"twitter-tweet\" data-width=\"220\"><p lang=\"fr\" dir=\"ltr\">PSG : Zlatan Ibrahimovic honoré par la mairie de Paris <a href=\"https://t.co/kzhVSr0EPz\">https://t.co/kzhVSr0EPz</a> <a href=\"https://t.co/IlH1EbKfie\">pic.twitter.com/IlH1EbKfie</a></p>— L'ÉQUIPE (@lequipe) <a href=\"https://twitter.com/lequipe/status/734278660237070336\">May 22, 2016</a></blockquote><script async src=\"//platform.twitter.com/widgets.js\" charset=\"utf-8\"></script>
As you can see, I can't retrieve the image url (pic.twitter.com/IlH1EbKfie redirects automatically to the Twitter status).
Thanks again for your help!
Hi @mnguye08, You can use the filter: acf/rest_api/{type}/get_fields and the function ( get_status_image ). Add these codes in your functions.php
function get_status_image( $html ) {
$matches = null;
if ( preg_match_all( '/https:\/\/t.co\/[a-zA-Z0-9]+/', $html, $matches ) ) {
$html = file_get_contents( end( $matches[0] ) );
if ( preg_match( "/data-image-url=\"(.*)\"/", $html, $matches ) && count( $matches ) == 2 ) {
return $matches[1];
}
}
return false;
}
add_filter( 'acf/rest_api/post/get_fields', function( $data ) {
if ( method_exists( $data, 'get_data' ) ) {
$data = $data->get_data();
} else {
$data = (array) $data;
}
if ( isset( $data['acf'] ) && isset( $data['acf']['oembed_field'] ) ) {
$data['acf']['oembed_image'] = get_status_image( $data['acf']['oembed_field'] );
}
return $data;
} );
oembed_field is the acf field name.
Result:
Thanks
Hi @airesvsg,
thanks to take time to find a solution!
I've tried to use the filter
{
"code": "rest_no_route",
"message": "Aucun itinéraire n’a été trouvé correspondant à l’URL et à la méthode de requête",
"data": {
"status": 404
}
}
For information (I don't know if it's linked), when I use this url "
{
"acf": false
}
An other point: the twitter oembed field named "tweet_url" by my side is under a flexible content named "footer".
Thanks again!
Hi @mnguye08, please change the filter to:
add_filter( 'acf/rest_api/option/get_fields', function( $data ) {
if ( method_exists( $data, 'get_data' ) ) {
$data = $data->get_data();
} else {
$data = (array) $data;
}
if ( isset( $data['acf'] ) && isset( $data['acf']['footer'] ) && ! empty( $data['acf']['footer'] ) ) {
foreach( $data['acf']['footer'] as &$f ) {
$f['tweet_url_img'] = get_status_image( $f['tweet_url'] );
}
}
return $data;
} );
Thanks
Hi @airesvsg, I finally use the 1st filter ("/acf/rest_api/post/get_fields") and it works like a charm! Thanks again for all your help!
Hi, I've a question about ACF & oEmbed field. Is there a way to retrieve media url from a Twitter status in an oEmbed field (by modifying/overriding the JSON response)? Thanks for your feedback. Kind regards