WP Rest API- POST request for images returns as empty brackets [ ]

Viewed 287

Iam new to php so I would appreciate your help.

Below code provides for rest api responses for taxonomy, metafields and gallery/image field.

When i issue a POST request via API, the image field returns as empty while the taxonomy and other meta fields return new value as expected.

As can be seen from below response json, the return value is []

InputJson

{"Taxonomy1":[445],"meta":{'field1':'55'},"Imagefield":['/home/Imagefolder/1.jpeg']}

ResponseJson

{"Taxonomy1":[445],"meta":{'field1':'55'},"Imagefield":[]}

Note: Imagefield is instance of GalleryField

I cant see why the Imagefield gives empty field ([]). Is there anything that sticks out in the below code that needs to be edited? Any advise?

<?php

namespace Dehaka\Managers;

use Dehaka\Core\Manager;
use Dehaka\Model\Post\Car;
use Dehaka\Model\Post\Field\AttachmentsField;
use Dehaka\Model\Post\Field\Field;
use Dehaka\Model\Post\Field\GalleryField;
use Dehaka\Model\Post\Field\NumberField;
use Dehaka\Model\Post\Field\Price\PriceField;
use Dehaka\Model\Post\Field\Taxonomy\Taxonomy;
use WP_Post;

class RestApiManager extends Manager
{

    public function boot()
    {
        add_action('rest_api_init', [$this, 'registerFields']);

        add_action('rest_insert_' . Car::POST_TYPE, function (WP_Post $post, $request) {
            $meta = $request->get_param('meta');
            $car = new Car($post);

            dehakaApp('car_fields')
                ->filter(static function ($field) use ($meta) {
                    /* @var Field $field */
                    return !$field instanceof Taxonomy  && isset($meta[$field->getKey()]);
                })
                ->each(static function ($field) use ($car, $meta) {
                    $field->save($car, $meta[$field->getKey()]);
                });
        }, 10, 2);
    }

    public function registerFields()
    {
        foreach (dehakaApp('car_fields') as $field) {
            if ($field instanceof Taxonomy) {
                continue;
            }
            
            

            register_rest_field(Car::POST_TYPE, $field->getKey(), [
                'get_callback' => function ($post) use ($field) {
                    if (!$field instanceof GalleryField && !$field instanceof AttachmentsField && !$field instanceof PriceField) {
                        return get_post_meta($post['id'], $field->getKey(), true);
                    }

                    if ($field instanceof PriceField) {
                        $car = Car::getById($post['id']);
                        if (!$car) {
                            return [];
                        }

                        return $field->getValue($car);
                    }


                    $ids = explode(',', get_post_meta($post['id'], $field->getKey(), true));
                    if ($field instanceof GalleryField) {
                        $images = [];

                        foreach ($ids as $id) {
                            $imageUrl = wp_get_attachment_image_url($id, 'full');
                            if ($imageUrl) {
                                $images[] = $imageUrl;
                            }
                        }

                        return $images;
                    }

                    
                },
            ]);
        }
    }

###SOLUTION

I found the solution. All i had to do was insert image id instead of image url in the "Imagefield". Hence as follows:

inputjson={"Taxonomy1":[445],"meta":{'field1':'55'},"Imagefield":<image id number>}
0 Answers
Related