How to upload wordpress front-end pictures
This article mainly introduces how to upload wordpress front-end pictures, the article is very detailed, has a certain reference value, interested friends must read it!
Upload pictures on wordpress frontend.
Recently, a research project needs to upload user profile photos on the front end of wordpress, and look up some information on the Internet! Solved the problem!
1: first of all, add the file upload box where you need it.
2: process the picture
$post=get_post (13); / if for testing ($_ FILES) {$files = $_ FILES ['files']; $count= count ($files [' name']) Foreach ($files ['name'] as $key = > $value) {if ($files [' name'] [$key]) {$file = array ('name' = > $files [' name'] [$key], 'type' = > $files [' type'] [$key], 'tmp_name' = > $files [' tmp_name'] [$key]) 'error' = > $files [' error'] [$key], 'size' = > $files [' size'] [$key]) $_ FILES = array ("files" = > $file); foreach ($_ FILES as $file = > $array) {$newupload = insert_attachment ($file,$post- > ID); / / this method appends articles to articles with ID 13. If you do not want to insert into the article, it can be empty ""}}.
3: add function to functions.php file
Insert_attachment the second argument to this function will not be appended to the picture in the article if it is empty. Function insert_attachment ($file_handler,$post_id,$setthumb='false') {global $wpdb; / / check to make sure its a successful upload if ($_ FILES [$file_handler] ['error']! = = UPLOAD_ERR_OK) _ _ return_false (); require_once (ABSPATH. "wp-admin". / includes/image.php'); require_once (ABSPATH. "wp-admin". / includes/file.php'); require_once (ABSPATH. "wp-admin". / includes/media.php'); $attach_id = media_handle_upload ($file_handler, $post_id); $image_url = wp_get_attachment_image_src ($attach_id,'full'); if ($setthumb) {$wpdb- > insert ($wpdb- > prefix. 'postmeta', array (' post_id' = > $post_id, 'meta_key' = >' wpcf-vi-img', 'meta_value' = > $image_url [0]));} return $attach_id;}
4: reference method
$image_url = wp_get_attachment_image_src ($attach_id,'full'); / / it doesn't work to use this method directly on the page because of page refresh problems! You need to construct the functionality of this method in the function. / / the method of cycling the feature pictures in the article, if you attach the picture to the article, you can use this method to output in batch! Foreach ($imagess as $images) {echo $images;} is all the contents of this article entitled "how to upload wordpress front-end images". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!