나는 워드 프레스의 라이브러리 내부에 업로드 된 사진을 삽입 제대로 작동이 코드를 가지고 성공적설정 기능을 갖춘 이미지는 워드 프레스
게시물을 작성하지만이 기능을 갖춘 이미지로 업로드 된 사진을 설정하기 위해 작동하지 않았다 게시물에
참고 : 사용자 정의 게시물 유형을 사용합니다.
난 당신이 that.Hope 같은 파일 경로가 당신을 위해 작동합니다 확인하십시오 수
$dir = plugin_dir_path(__FILE__);
$file_path = $dir."/uploads/";
$text = $_POST['text'];
$user = $_POST['usr'];
$file_path = $file_path . basename($_FILES['uploaded_file']['name']);
move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path);
$file = $file_path;
$filename = basename($file);
$upload_file = wp_upload_bits($filename, null, file_get_contents($file));
if (!$upload_file['error']) {
$wp_filetype = wp_check_filetype($filename, null);
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_parent' => $parent_post_id,
'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attachment_id = wp_insert_attachment($attachment, $upload_file['file'], $parent_post_id);
if (!is_wp_error($attachment_id)) {
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
$attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']);
wp_update_attachment_metadata($attachment_id, $attachment_data);
}
}
unlink($file_path);
$user = get_userdatabylogin($_POST['usr']);
$user_ID = $user->ID; // prints the id of the user
global $user_ID;
$new_post = array(
'post_title' => 'My New Post',
'post_content' => $_POST['text'],
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'taken_photos',
'post_category' => array(0)
);
$pid = wp_insert_post($new_post);
update_post_meta($pid, '_thumbnail_id', $attachment_id);
$attachment_data = array(
'ID' => $attachment_id,
'post_excerpt' => 'TITLE'
);
은'$의 file_path'는이 파일의 경로가 일시적이며, 그것과는 상관이 없다 당신은 코드에 좋은 모습을 가지고가는 경우 이 파일이 wordpress 미디어 라이브러리에 삽입 된 후이 파일을 삭제하는 것을 볼 수 있습니다. –