最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - Upload images to custom database table in admin backend

programmeradmin0浏览0评论

I have a custom post type that inserts user details into a custom table from a form in the WordPress back-end. Here's the code for the form:

function display_add_user_metabox(){
  echo
  '<form method="post" enctype="multipart/form-data" action="'.htmlspecialchars($_SERVER['PHP_SELF'] ).'">',
      '<p><label for"firstname"  >First Name</label>',
      '<input type="text" name="firstname"></p>',
      '<p><label for"surname"  >Last Name</label>',
      '<input type="text"  name="surname"  > </p>',
      '<label for"nickname">Nickname</label>',
      '<input type=nick"text"  name="nickname" ></p>',
      '<label for"userpic">user Image</label>',
      '<input type="file"  name="userpic" ></p>',
      '<input type ="submit" name="submit" value="Add user" >',
   '</form>';
}

and the function to insert the users:

function add_user(){

   global $wpdb;
   $table_name = $wpdb->prefix . "users";
   $name       = $_POST['firstname'];
   $surname    = $_POST['surname'];
   $nickname   = $_POST['nickname'];
   $upload     = wp_upload_bits($_FILES["userpic"]["name"], null, file_get_contents($_FILES["userpic"]["tmp_name"]));


     $wpdb->insert(
           $table_name,
           array(
             'user_name'       => $name,
             'user_surname'    => $surname,
             'user_nickname'   => $nickname,
             'user_image'      => $upload['url']
           ),
           array(
             '%s',
             '%s',
             '%s',
             '%s'
           )
         );

}
if( isset($_POST['submit']) ) {
   add_user();
}

This doesn't insert anything in the table. However if I comment out the $upload or anything related to the image, everything works fine. What am I missing?

I have a custom post type that inserts user details into a custom table from a form in the WordPress back-end. Here's the code for the form:

function display_add_user_metabox(){
  echo
  '<form method="post" enctype="multipart/form-data" action="'.htmlspecialchars($_SERVER['PHP_SELF'] ).'">',
      '<p><label for"firstname"  >First Name</label>',
      '<input type="text" name="firstname"></p>',
      '<p><label for"surname"  >Last Name</label>',
      '<input type="text"  name="surname"  > </p>',
      '<label for"nickname">Nickname</label>',
      '<input type=nick"text"  name="nickname" ></p>',
      '<label for"userpic">user Image</label>',
      '<input type="file"  name="userpic" ></p>',
      '<input type ="submit" name="submit" value="Add user" >',
   '</form>';
}

and the function to insert the users:

function add_user(){

   global $wpdb;
   $table_name = $wpdb->prefix . "users";
   $name       = $_POST['firstname'];
   $surname    = $_POST['surname'];
   $nickname   = $_POST['nickname'];
   $upload     = wp_upload_bits($_FILES["userpic"]["name"], null, file_get_contents($_FILES["userpic"]["tmp_name"]));


     $wpdb->insert(
           $table_name,
           array(
             'user_name'       => $name,
             'user_surname'    => $surname,
             'user_nickname'   => $nickname,
             'user_image'      => $upload['url']
           ),
           array(
             '%s',
             '%s',
             '%s',
             '%s'
           )
         );

}
if( isset($_POST['submit']) ) {
   add_user();
}

This doesn't insert anything in the table. However if I comment out the $upload or anything related to the image, everything works fine. What am I missing?

Share Improve this question asked Jan 4, 2017 at 8:56 Pamela SillahPamela Sillah 35 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Include the following before global $wpdb;

 require_once(ABSPATH . "wp-admin" . '/includes/image.php');
 require_once(ABSPATH . "wp-admin" . '/includes/file.php');
 require_once(ABSPATH . "wp-admin" . '/includes/media.php');
发布评论

评论列表(0)

  1. 暂无评论