• Resolved Nexxoz

    (@nexxoz)


    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;
    	}
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Nexxoz

    (@nexxoz)

    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;
    	}
    Thread Starter Nexxoz

    (@nexxoz)

    Code above works

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[HELP] Upload to MEDIA library’ is closed to new replies.