How To Upload Image In Wordpress Code
How to Add together Images to Media Library after Uploading them in PHP Programmatically
When you upload files from WordPress admin area, they will automatically appear in Media > Library. But what if you want to create a custom file uploader especially for your website visitors?
Stride ane. As usual, we Begin with HTML Form
<form activity="<?php echo get_stylesheet_directory_uri() ?>/process_upload.php" method="postal service" enctype="multipart/form-information"> Your Photograph: <input type="file" name="profilepicture" size="25" /> <input type="submit" proper name="submit" value="Submit" /> </grade>
What is important to remember hither?
- I placed the PHP file
process_upload.php
into the theme directory, if you would like to move information technology to the other location, do not forget to make changes in the next piece of code on line iv (I mean lawmaking in Stride two). - The form must have
enctype="multipart/course-data"
aspect. Many forget near information technology.
You can too create a shortcode for this class, just insert this to the functions.php
:
add_shortcode( 'misha_uploader', 'misha_uploader_callback' ); function misha_uploader_callback(){ render '<form action="' . get_stylesheet_directory_uri() . '/process_upload.php" method="post" enctype="multipart/form-information"> Your Photo: <input type="file" name="profilepicture" size="25" /> <input type="submit" proper noun="submit" value="Submit" /> </form>'; }
Now yous can utilize just [misha_uploader]
in the editor.
Pace 2. Procedure the Uploaded File in PHP and Add the File Metadata to WordPress Database
This is process_upload.php
file that I decided to create in my current theme directory.
<?php // WordPress surround require( dirname(__FILE__) . '/../../../wp-load.php' ); $wordpress_upload_dir = wp_upload_dir(); // $wordpress_upload_dir['path'] is the full server path to wp-content/uploads/2017/05, for multisite works good every bit well // $wordpress_upload_dir['url'] the accented URL to the same folder, really we do not demand it, but to evidence the link to file $i = 1; // number of tries when the file with the aforementioned proper noun is already exists $profilepicture = $_FILES['profilepicture']; $new_file_path = $wordpress_upload_dir['path'] . '/' . $profilepicture['name']; $new_file_mime = mime_content_type( $profilepicture['tmp_name'] ); if( empty( $profilepicture ) ) die( 'File is not selected.' ); if( $profilepicture['fault'] ) die( $profilepicture['error'] ); if( $profilepicture['size'] > wp_max_upload_size() ) die( 'It is too large than expected.' ); if( !in_array( $new_file_mime, get_allowed_mime_types() ) ) die( 'WordPress doesn\'t let this type of uploads.' ); while( file_exists( $new_file_path ) ) { $i++; $new_file_path = $wordpress_upload_dir['path'] . '/' . $i . '_' . $profilepicture['proper name']; } // looks like everything is OK if( move_uploaded_file( $profilepicture['tmp_name'], $new_file_path ) ) { $upload_id = wp_insert_attachment( array( 'guid' => $new_file_path, 'post_mime_type' => $new_file_mime, 'post_title' => preg_replace( '/\.[^.]+$/', '', $profilepicture['proper name'] ), 'post_content' => '', 'post_status' => 'inherit' ), $new_file_path ); // wp_generate_attachment_metadata() won't piece of work if you practice not include this file require_once( ABSPATH . 'wp-admin/includes/image.php' ); // Generate and save the zipper metas into the database wp_update_attachment_metadata( $upload_id, wp_generate_attachment_metadata( $upload_id, $new_file_path ) ); // Testify the uploaded file in browser wp_redirect( $wordpress_upload_dir['url'] . '/' . basename( $new_file_path ) ); }
Optionally we can set the third parameter of the wp_insert_attachment()
as a parent postal service ID, and ready the featured prototype of this postal service with set_post_thumbnail()
.
Misha Rudrastyh
I develop websites since 2008, so it is total of 14 years of experience, oh my gosh. About of all I love dearest love to create websites with WordPress and Gutenberg, some ideas and thoughts I share throughout my web log.
Need some developer help? Contact me
Follow me on Twitter
Source: https://rudrastyh.com/wordpress/how-to-add-images-to-media-library-from-uploaded-files-programmatically.html
Posted by: hannapromestruche.blogspot.com
0 Response to "How To Upload Image In Wordpress Code"
Post a Comment