WordPress.org

Ready to get started?Download WordPress

Forums

Using media_handle_upload from the Front-End and Attaching Media to a Post (6 posts)

  1. Ryan
    Member
    Posted 3 years ago #

    Hi All,
    I am trying to design a file upload from the front-end of my wordpress theme and have a small, but annoying issue that I have run into. I am using a custom post type and trying to attach files to this post type. My code for the Form itself and the handler is below.

    The upload itself works and the media is placed into the correct directory and the media browser but it is NOT attached to any post. I am not sure where I am going wrong here and the code is amazingly brief on media_handle_upload so anyone with experience with this would be awesome....

    //**************************************
    // Attachment Insert Form
    //**************************************
    function insert_attachment_form($postID) {
    ?>
    	<form id="file-form" name="file-form" method="POST" action="" enctype="multipart/form-data" >
    		<input type="file" id="async-upload" name="async-upload" />
    		<input type="hidden" name="postID" value="<?php echo $post->ID ?>" />
    		<?php wp_nonce_field('client-file-upload', 'client-file-upload'); ?>
    		<input type="submit" value="Upload" id="submit" name="submit" />
    	</form>
    <?php } 
    
    //**************************************
    // Process Attachment Form
    //**************************************
    function process_attachment() {
    	// verify this came from the our screen and with proper authorization,
    	// because save_post can be triggered at other times
    	if ( !wp_verify_nonce( $_POST['client-file-upload'], 'client-file-upload') ) {
            return $post->ID;
        }
    
        // Is the user allowed to edit the post or page?
    	if ( !current_user_can( 'publish_posts', $post->ID ))
    		return $post->ID;
    
    	if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_FILES )) {
    		require_once(ABSPATH . 'wp-admin/includes/admin.php');
    		$id = media_handle_upload('async-upload', $_POST['postID']);
    		unset($_FILES);
    	}
    }
  2. Ryan
    Member
    Posted 3 years ago #

    Thank you everyone for the support! Lol, as soon as I published this I realized an error an it fixed the issue...since there is a minimum amount of information on the web about how to do this...here is my working code...again this is for a Front-End submission of an attachment in a wordpress theme...

    //**************************************
    // Attachment Insert Form
    //**************************************
    function insert_attachment_form($postID) {
    ?>
    	<form id="file-form" name="file-form" method="POST" action="" enctype="multipart/form-data" >
    		<input type="file" id="async-upload" name="async-upload" />
    		<input type="hidden" name="postID" value="<?php echo $postID; ?>" />
    		<?php wp_nonce_field('client-file-upload', 'client-file-upload'); ?>
    		<input type="submit" value="Upload" id="submit" name="submit" />
    	</form>
    <?php } 
    
    //**************************************
    // Process Attachment Form
    //**************************************
    function process_attachment() {
    	// verify this came from the our screen and with proper authorization,
    	// because save_post can be triggered at other times
    	if ( !wp_verify_nonce( $_POST['client-file-upload'], 'client-file-upload') ) {
            return $post->ID;
        }
    
        // Is the user allowed to edit the post or page?
    	if ( !current_user_can( 'publish_posts', $post->ID ))
    		return $post->ID;
    
    	if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_FILES )) {
    		require_once(ABSPATH . 'wp-admin/includes/admin.php');
    		$id = media_handle_upload('async-upload', $_POST['postID']);
    		unset($_FILES);
    	}
    }
  3. fontepink
    Member
    Posted 3 years ago #

    Please help, I´m interested to implement this code. I´m programing blog to control of maintenance in my office and I need to attachment files to post. Better from forntend.

    How implement this code in a single.php ??

    Thanks

  4. Ryan
    Member
    Posted 3 years ago #

    What trouble are you having?

  5. fontepink
    Member
    Posted 3 years ago #

    I have a single post with a description of a problem. I need to attach some files like images, pdf´s, etc. from frontend.

    Where can I paste your code to run the form in a single.php and how I call this functions?

    thanks.

  6. RightCoastRipper
    Member
    Posted 2 years ago #

    fontepink -

    use exec-PHP to use php scripted right into the post/page. call the page where the script is in the action variable in the form to pass the variables

    thanks for the info Ryan. Like you said, I couldn't find much info on media_handle_upload.

Topic Closed

This topic has been closed to new replies.

About this Topic