Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- http://cube3x.com/2013/03/upload-files-to-wordpress-media-library-using-php/
- <form action="upload_file.php" method="post" enctype="multipart/form-data">
- <label for="file">Filename:</label>
- <input type="file" name="myfile" id="myfile">
- <input type="submit" name="submit" value="Submit">
- </form>
- <?php
- 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 ) {
- //file is uploaded successfully. do next steps here.
- }
- 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);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement