0

I'm getting this in my json response

"main_image": 
[
"11125,11122,11123,11127,11128"
],

Those are the ID's of my post, I need to get the URL of each ID in the 'thumbnail' size. I did't find any solution, just this code but It's more for Custom Meta of a plugin.

function xxx_past_poss_custom_metadata( $post_response, $post, $context ) {
    $meta = get_post_custom( $post['ID'] );
    $custom_fields = array();
    $xxx_image_gallery = array();

    foreach ( $meta['xxx_cmb_image_gallery'] as $key => $value ) {
        $value = wp_get_attachment_url($value);
        $xxx_image_gallery[ $key ] = $value;
    };

    foreach ( $meta as $key => $value ) {
        // Replace 'xxx_' with any custom metakey prefix (ie. '_' for private metadata's)
        if ( 'xxx_' !== substr( $key, 0, 1 ) ) {
            $custom_fields[ $key ] = $value;
        };
    }

    $post_response['xxx_image_gallery'] = $xxx_image_gallery;
    $post_response['custom_fields'] = $custom_fields;
    return $post_response;
}
add_filter( 'json_prepare_post', 'xxx_past_poss_custom_metadata', 10, 3 );

My meta it's called "media_gallery".

Any help will be very appreciated.

[UPDATE]

Seems like I wasn't even modifying my json response because of a plugin I had activated, turning it off made me declare my meta in my functions.php file and no I have this.

add_action('rest_api_init', 'register_custom_fields', 1, 1);

function register_custom_fields(){

  register_rest_field(
    'job_listing',
    'thumbnail',
    array(
      'get_callback' => 'show_image'
    )
  );

}

function show_main_image($object, $field_name, $request){
  $custom_fields = get_post_custom($object['id']);
  $main_image = $custom_fields['main_image'];

  return $main_image;
}

I can now modify the response but I still haven't been able to turn all this ID's: "11125,11122,11123,11127,11128" into URL sources.

My custom input saves the images id's like this: "11125,11122,11123,11127,11128", so was Pedro said is true it

is an array with a single string inside

What I tried next was this:

function show_main_image($object, $field_name, $request){
  $custom_fields = get_post_custom($object['id']);
  $main_image = $custom_fields['main_image'];

  foreach ( $main_image as $key => $value ) {
      $imagesID = explode(',',$value);
      foreach ($imagesID as $id => $value) {
        $result = wp_get_attachment_url($value);
        $custom_fields[ $id ] = $result;
      }
  };
  $image_urls = array();

  return $result;
}

But still no luck, now the "main_image" endpoint appears to be empty.

  • Are the IDs the images in the post? Have you looked at wp_get_attachment_image_src? It might be what you need: developer.wordpress.org/reference/functions/… – Pedro Coitinho Jan 19 '17 at 10:35
  • Yes, they're in the post, in a custom field named "main_image". The response shows the array value but I can't turn them into URL. – Al Rosado Jan 20 '17 at 5:32
2

This is the correct way to achieve it is this:

function show_main_image($object, $field_name, $request){
    $custom_fields = get_post_custom($object['id']);
    $main_image = $custom_fields['main_image'];
    $arregloimg = explode(',',$main_image[0]);

    $image_urls = array();
    foreach ( $arregloimg as $key => $value ) {
        $image_urls[] = wp_get_attachment_url($value,'thumbnail');
    };

    return $image_urls;
}
0

based on your comment I think what you need is wp_get_attachment_image_src.

Wordpress saves attachments ( like images ) as a type of post. Might seem silly, but this means it saves all sorts of meta information about it, such as diferent sizes, captions etc. As a result, each image gets a post ID.

So assuming you have this array, which is parsed from your JSON:

$main_image = array( 11125, 11122, 11123, 11127, 11128 );

( Note that it seems your JSON is returning an array witha single string inside, instead of multiple integers )

You simply go through requesting the image URL at thumbnail size:

$image_urls = array();

foreach ( $main_image as $image_id ) {
    $image_urls[] = wp_get_attachent_image_src( $image_id, 'thumbnail' );
}

Now $image_urls is populated with ... image urls :)

Is that what you were looking for?

  • Hi Pedro, thanks for your help. I tried your code but didn't worked. I will elaborate my explanation a bit more. Seems like I wasn't even modifying my json response because of a plugin I had activated, turning it off made me declare my meta in my functions.php file and no I have this. (Code included in the Question update. – Al Rosado Jan 22 '17 at 5:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.