• Resolved paraddictednet

    (@paraddictednet)


    I love your plugin, simple yet far better than any commercial version.

    I have a problem: I would like to store the uploaded images together with the post, i.e. setting the $post->ID of the respective post as post_parent of the attachment.

    I tried to include

    global $post;
    $id = $post->ID;
    wp_update_post(array(
    'ID' => $attach_id,
    'post_parent' => $cpt_id
    ));

    in the handle_file function, but the $post variable is empty at this point.

    Within the html.php, the $post->ID is still valid, but it seems to get lost in the functions… Since I am using a custom post type, even url_to_postid() is not working to get the ID.

    Any help would be appreciated.
    Thanks
    Hannes

    http://wordpress.org/extend/plugins/alchemist-ajax-upload/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter paraddictednet

    (@paraddictednet)

    Solved.
    For anyone who wants to do this too (useful if you include a gallery with all the post’s images in your template), replace the function handle_file with the following code:

    function handle_file($upload_data)
        {
    		$slug = '';
    		$slug = basename($_SERVER['HTTP_REFERER']);
    		global $wpdb;
    		$id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$slug'");
            $return = false;
            $uploaded_file = wp_handle_upload($upload_data, array('test_form' => false));
    
            if (isset($uploaded_file['file'])) {
                $file_loc = $uploaded_file['file'];
                $file_name = basename($upload_data['name']);
                $file_type = wp_check_filetype($file_name);
    
                $attachment = array(
                    'post_mime_type' => $file_type['type'],
                    'post_title' => preg_replace('/\.[^.]+$/', '', basename($file_name)),
                    'post_content' => '',
                    'post_status' => 'inherit',
                    'post_parent' => $id
                );
    
                $attach_id = wp_insert_attachment($attachment, $file_loc, $id);
                $attach_data = wp_generate_attachment_metadata($attach_id, $file_loc);
                wp_update_attachment_metadata($attach_id, $attach_data);
                $return = array('data' => $attach_data, 'id' => $attach_id);
                return $return;
            }
    
            return $return;
        }

    IMPORTANT: your permalinks must end with the post’s slug to make this work.

    @paraddictednet, I would like to do this but could you please elaborate on your last statement “permalinks must end with the post’s slug to make this work”. I hope you are talking about pretty permalinks where the posts url are not according the year, month, date… or so but based on actual words in the title.

    Another important thing I would like to get help with is, I have a frontend post creator form. Do you think this hack could work with the form so the the uploaded images will be attached to the posts. If so please be kind and explain.
    Cheers.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Store images with post?’ is closed to new replies.