Home > CMS Tutorial > WordPress > How to upload images on WordPress front-end

How to upload images on WordPress front-end

藏色散人
Release: 2021-06-27 16:24:48
forward
2773 people have browsed it

WordPress Tutorial column will introduce to you how to upload images on the WordPress front-end.

Recently researching a project requires uploading user avatars on the WordPress front-end, and I checked some information on the Internet! solved this problem!
1: The first thing is to add the file upload box where needed

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
  <input type="submit" name="submit" value="Upload!" />
</form>
Copy after login

2: Process the image

$post=get_post(13);//测试用

if ( $_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);//此方法将文章附加到ID为13的文章中。如果不想插入到文章可以为空""
} } } }
Copy after login

3: Add the function function to the functions.php file

insert_attachment该函数的第二个参数如果为空将不附加到文章中图片。
Copy after login
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;
}
Copy after login

4: Reference method

$image_url = wp_get_attachment_image_src(  $attach_id,'full' );//由于页面刷新的问题直接在页面使用这个方法是不生效的!需要在函数中构造此方法的功能。

//循环文章中的特征图片的方法,如果将图片附加到文章中使用这个方法可以批量输出!
$imagess=get_post_meta(13,'wpcf-vi-img',false);
foreach($imagess as $images){
 echo  $images;
}
Copy after login

The above is the detailed content of How to upload images on WordPress front-end. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template