• I am using wp_editor as a front end editor to create posts. The media_buttons works great for uploading photos and video to the media library, but they are not attached to the post. Is there a way to grab the attachment_id when the file is uploaded so I can attach it to the newly created post?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The general idea is this:

    1. before wp_editor(), create a post (probably an auto-draft)
    2. get the ID of the post you just created
    3. set global $post_ID; to that new ID (save the the original ID so you can restore it in a bit)
    4. the $post_ID global is used inside get_upload_iframe_src()
    5. use wp_editor()
    6. restore post_ID

    Better ideas welcome.

    (I know we spoke in IRC, but this way we wont have this.)

    I am having a similar problem. I am using the wp_editor() to render the editor on settings pages and meta boxes in custom post types.

    After I click the media upload button, on meta boxes the media upload doesn’t even have an “add to post” button, only a “use as featured image” link.

    On settings pages, the uploader shows the “add to post” button, but it doesn’t actually add the image. Any solutions?

    I ended up finding a good solution to this problem elsewhere in the forums, here.

    Basically, you just add this to your functions.php file:

    add_filter( 'get_media_item_args', 'force_send' );
    function force_send($args){
    	$args['send'] = true;
    	return $args;
    }

    This worked for me.

    My problem also was caused by a a javascript I had to call the media upload function for a custom meta box. This blog post describes how to solve that problem by ensuring WordPress uses the default function to insert media into the post.

    to jrav001 question and Kaileys challenge:
    just tap to the filter in get_upload_iframe_src() function like so

    add_filter('_upload_iframe_src', 'filter_media_upload_iframe_src');
    function filter_media_upload_iframe_src($src) {
      $id = $post_id_im_editing_with_the_wp_editor_in_frontend;
      return add_query_arg(array("post_id" => $id), $src);
    }

    The global $post_ID doesn’t get set in the frontend which is normally the source for post_id argument in the upload_iframe_src link ??

    Is there a way to do this without having the post ID first?

    Maybe by grabbing the attachment IDs of each uploaded attachment and then assigning the IDs to the post after as it’s published?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘wp_editor media upload’ is closed to new replies.