• Resolved smarie33

    (@smarie33)


    Been searching for a couple of hours and couldn’t find anything.

    So I am using wp_editor() to create an easy way to post on the front end. When users upload images through the media uploader they do not attach to the post they are creating and I would really like this.

    Images are uploaded before the wp_editor() get processed. I just don’t know how to pass the post’s id to the images that are being uploaded. I want there to be a gallery in the media uploader for each post where there are images uploaded with it like in the backend, so I don’t want to use a custom field. I’d rather have the post’s id passed into the image’s attachment metadata as the post_parent, but I can’t figure out how to do this while or after the image uploads or even after the wp_editor processes. I know there is attachment meta because I see that there is an author and date in the media library. When I try to wp_update_attachment_metadata I find that I cannot pass the drafted post id back to my plugin where there is a wp_insert_post hook looking for an ‘inherit’ post status. All of the examples I find online of attaching media uploads to post deal with building your own upload form, not hooking into the media uploader.

    So is there any other way that I should be doing this? Do I really just need to make custom meta and create my own gallery tab in the media uploader? Is there a way to already have the wp_editor associated with a post id before processing it? Below is my code:

    In my plugin (plugin page): obviously this doesn’t really work because I’m not passing any variable back, but maybe it will work if someone can tell me how to pass a variable back:

    add_action('wp_insert_post', array(&$this, 'process_images'));
        function process_images($results){
        //global $emptynew;
            if($results['post_status'] == 'inherit'){
              $attachment = array(
              'post_parent' => $emptynew,
            );
            wp_update_attachment_metadata( $results['ID'],  $attachment );
           };
        }

    In my page where people post (theme page)

    get_currentuserinfo();
    $loggedInNow = $current_user->ID;
    
    $emptynew = wp_insert_post(array(
        'comment_status' => 'closed',
        'post_title' => 'waiting to be edited by user '.$loggedInNow,
        'post_content' => '',
        'post_author' => $loggedInNow,
        'post_status' => 'draft',
        'post_type' => 'post',
        )
    );
     <form action="<?php echo home_url('/').'thank-you/'; ?>" method="post">
                    <label for="title">Title: </label><input size="80" type="text" name="postTitle" /><br />
    <?php
    wp_editor($contentFromPage, 'responsesubmit', $settings = array('media_buttons' => true));
    ?>
     <input type="hidden" name="postempty" value="<?php echo $emptynew; ?>" />
     <br /><input type="submit" value="Submit" />
      </form>

    In the page where the post is processed (theme page)

    <?php
                    //information sent from the response pages that have the template Response Template page
    get_currentuserinfo();
    $loggedInNow = $current_user->ID;
    $response = wp_insert_post(array(
    'ID' => $_POST['postempty'],
    'comment_status' => 'closed',
    'post_title' => $_POST['postTitle'],
    'post_content' => $_POST['responsesubmit'],
    'post_author' => $loggedInNow,
    'post_status' => 'publish',
    'post_type' => 'post'
    );
Viewing 2 replies - 1 through 2 (of 2 total)
  • Well the first problem you have is method=”post” should be the enctype multipart/form-data or else it’s never even going to see the upload.

    This documentation is used for the wp admin meta boxes but you can probably strip some information out of it that is pertinent to what you are trying to do:

    https://www.trovster.com/blog/2011/07/wordpress-custom-file-upload

    Thread Starter smarie33

    (@smarie33)

    thank you for your reply. i could not get the code from that link to work with the media uploader. everywhere i look for this solution they all create a new upload form. i can’t use that code because i can’t grab the uploaded image url from the media uploader without javascript and that creates a whole bunch of other problems for me, (i want the media uploader to handle all the image processing anyway). but i finally found my solution here: https://www.ads-software.com/support/topic/wp_editor-media-upload?replies=6

    now the code from my theme page where the post is processed looks like this

    <?php
     //information sent from the response pages that have the template Response Template page
    get_currentuserinfo();
    $loggedInNow = $current_user->ID;
    $response = wp_insert_post(array(
    'ID' => $_POST['postempty'],
    'comment_status' => 'closed',
    'post_title' => $_POST['postTitle'],
    'post_content' => $_POST['responsesubmit'],
    'post_author' => $loggedInNow,
    'post_status' => 'publish',
    'post_type' => 'post'
    );
    
     global $post_ID;
     $post_ID = $emptynew;
    ?>

    i added enctype=”multipart/form-data” in my form with wp_editor in it for good measure. so simple, i wish this was documented, did not come across it in google easily.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘front-end attaching image to post (create gallery tab in media uploader)’ is closed to new replies.