WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] [HELP] Upload to MEDIA library (3 posts)

  1. nexxoz
    Member
    Posted 8 months ago #

    Im currently working on a plugin to my project and i want users to be able to upload images to the media library the problem is that the code dosen't make a thumbnail image or add watermark by the Easy watermark plugin, help plz

    if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
    	$uploadedfile = $_FILES['myfile'];
    	$upload_overrides = array( 'test_form' => false );
    	$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
    
    		if ( $movefile ) {
    		    $wp_filetype = $movefile['type'];
    		    $filename = $movefile['file'];
    		    $wp_upload_dir = wp_upload_dir();
    		    $attachment = array(
    		        'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
    		        'post_mime_type' => $wp_filetype,
    		        'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
    		        'post_content' => '',
    		        'post_status' => 'inherit'
    		    );
    		    $attach_id = wp_insert_attachment( $attachment, $filename);
    		    $text = 'Image uploaded! Waiting to be accepted by admins';
    		}
    
    	}
    
    	if(empty($text)){
    
    	echo '<form role="form" method="post" enctype="multipart/form-data">';
    	echo '  <div class="form-group">';
    	echo '    <label for="exampleInputFile">Välj fil</label>';
    	echo '    <input type="file" name="myfile">';
    	echo '   <p class="help-block">Minst 250x250px</p>';
    	echo '  </div>';
    	echo ' <button type="submit" name="ch-upload" class="btn btn-default">Ladda upp</button>';
    	echo '</form>';
    
    	}else{
    		echo $text;
    	}
  2. nexxoz
    Member
    Posted 8 months ago #

    Never mind, fixed the code.

    if(isset($_POST['ch-upload'])){
    	$text;
    
    	if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
    	$uploadedfile = $_FILES['myfile'];
    	$upload_overrides = array( 'test_form' => false );
    	$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
    
    		if ( $movefile ) {
    		    $wp_filetype = $movefile['type'];
    		    $filename = $movefile['file'];
    		    $wp_upload_dir = wp_upload_dir();
    		    $attachment = array(
    		    	'post_mime_type' => $wp_filetype,
    		        'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
    		        'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
    		        'post_content' => '',
    		        'post_status' => 'inherit'
    		    );
    
    		    // $attach_id = wp_insert_attachment( $attachment, $filename);
    		    // wp_update_attachment_metadata($attach_id, $filename);
    
    		    $attach_id = wp_insert_attachment( $attachment, $filename );
    
    			require_once(ABSPATH . 'wp-admin/includes/image.php');
    			require_once(ABSPATH . 'wp-admin/includes/media.php');
    			$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
    			wp_update_attachment_metadata( $attach_id, $attach_data );
    
    			image_resize($filename, 200, 200, true, '250x250');
    
    		    $text = 'Image uploaded! Waiting to be accepted by admins';
    		}
    
    	}
    
    	if(empty($text)){
    
    	echo '<form role="form" method="post" enctype="multipart/form-data">';
    	echo '  <div class="form-group">';
    	echo '    <label for="exampleInputFile">Välj fil</label>';
    	echo '    <input type="file" name="myfile">';
    	echo '   <p class="help-block">Minst 250x250px</p>';
    	echo '  </div>';
    	echo ' <button type="submit" name="ch-upload" class="btn btn-default">Ladda upp</button>';
    	echo '</form>';
    
    	}else{
    		echo $text;
    	}
  3. nexxoz
    Member
    Posted 8 months ago #

    Code above works

Reply

You must log in to post.

About this Topic