WordPress.org

Ready to get started?Download WordPress

Forums

JSON API
How can i create post with attachments? (4 posts)

  1. shinboiz
    Member
    Posted 1 year ago #

    Hi,
    I'm a newbie, so could someone helps me how can i create a post with attachments/images ?

    Thank you very much.

    http://wordpress.org/extend/plugins/json-api/

  2. ben.IT
    Member
    Posted 1 year ago #

    Hi,
    I am also concerned by his issue.
    Does anybody get an idea/snippet ?

  3. lego123
    Member
    Posted 1 year ago #

    Same here... I'v tried passing the attachment id, attachment object, then the attachment object as an array for the &attachment variable unfortunately nothing seems to stick. What gives?

  4. lego123
    Member
    Posted 1 year ago #

    Hi guys, just wanted to contribute a quick fix solution for what I was trying to achieve.

    I added the following code on line 121 of json-api/models/post.php

    if (isset($values['media'])) {
            foreach($values['media'] as $att){
              $attachment_id = wp_insert_attachment(get_post($att), false, $this->id);
              $this->attachments[] = new JSON_API_Attachment($attachment_id);
            }
        }

    This will allow you to pass an array of attachment id's to the &media variable and it will link them to the new post.

    I also wrote a simple custom controller called attachments/get_attachments to get unattached files as well. If you need to view unattached files just feed it a &parent=0, to see all files use &parent=null.

    json-api/controllers/attachments.php

    <?php
    /*
    Controller name: Attachments
    Controller description: Basic introspection methods for fetching attachments
    */
    
    class JSON_API_Attachments_Controller{
        public function get_attachments(){
            global $json_api;
    
            if($json_api->query->parent !== "null")
                $parent = (integer) $json_api->query->parent;
            else
                $parent = null;    
    
            return $json_api->introspector->get_attachments($parent);
    
        }
    }
    
    ?>

    I hope this helps.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic