php - 从前端上传自定义帖子类型中的问题

标签 php mysql wordpress

    <div class="modal-body">
      <form method="POST" action="">
          <input type="file" name="featured_img"><br/>
          <input type="submit" name="imgupdate" class="btn btn-info btn-lg">
          <button type="button" class="btn btn-info btn-lg" data-dismiss="modal">Cancel</button>
      </form>

    </div>


<?php
    global $wpdb;
    $update_id = get_the_ID();

    //    Update featured image

    $uploaddir  = wp_upload_dir();
    $file       = $_FILES['featured_img'];
    $uploadfile = $uploaddir['path'] . '/' . basename($file['name']);
    move_uploaded_file($file['tmp_name'], $uploadfile);
    $filename    = basename($uploadfile);
    $wp_filetype = wp_check_filetype(basename($filename), null);
    $attachment  = array(
        'post_mime_type' => $wp_filetype['type'],
        'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
        'post_content' => '',
        'post_status' => 'inherit',
        'menu_order' => $_i + 1000
    );
    $update_img  = wp_insert_attachment($attachment, $uploadfile);
    if (isset($_POST['imgupdate'])) {
        if (($_FILES['featured_img']['name'] == true)) {
            update_post_meta($update_id, '_thumbnail_id', $update_img);
        }
    }

?>

我想从前端编辑特色图像我想通过使用此代码来实现此目的,但它不起作用。它不会更新图像。有什么帮助吗?我在哪里遇到问题。此代码在其他页面上运行良好。但我想在模型中弹出这个。这在弹出窗口中不起作用。有什么帮助吗?

提前致谢。

最佳答案

你可以添加这样的代码,它对我有用。

希望这也对您有用。

 if ($_FILES) {
        array_reverse($_FILES);
        if ($_FILES['file_featured_image']) {
            $i = 0; //this will count the posts
            foreach ($_FILES as $file => $array) {
                if ($i == 0)
                    $set_feature = 1; //if $i ==0 then we are dealing with the first post
                else
                    $set_feature = 0; //if $i!=0 we are not dealing with the first post

                insert_featured_image_for_property($file, $property_id, $set_feature);
                $i++; //count posts
            }
        }
  }

在functions.php中添加一个函数

function insert_featured_image_for_property($file_handler, $property_id, $setthumb = 'false') {
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) {
    return __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, $property_id);
//set post thumbnail if setthumb is 1
if ($setthumb == 1)
    update_post_meta($property_id, '_thumbnail_id', $attach_id);
return $attach_id;
}

关于php - 从前端上传自定义帖子类型中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46662181/

相关文章:

php - 无需编辑 .js 文件的 Javascript onclick html 字段值

mysql - 为使用 C# 和 MySQL 开发的应用程序向客户提供软件更新的方法

php - 如何在不使用重音的情况下搜索有重音的名字?

mysql - 清理数据库、rake 任务或 sql 的更好方法是什么?

php - 用 PhP 中的可点击图像替换 woocommerce 中的按钮

php - 显示所有时间约会和保留

php - 如何从 HTTP_ACCEPT_LANGUAGE 订购字符串

php - 301重定向URL也被重写

css - 服务器切换后@font face停止工作

jquery - 如何将类添加到高度大于宽度的元素的父级