{"id":3090,"date":"2021-12-04T09:34:09","date_gmt":"2021-12-04T00:34:09","guid":{"rendered":"https:\/\/blue-bear.jp\/kb\/?p=3090"},"modified":"2021-12-04T09:34:11","modified_gmt":"2021-12-04T00:34:11","slug":"wordpress-%e7%94%bb%e5%83%8furl%e3%82%92%e6%8c%87%e5%ae%9a%e3%81%97%e3%81%a6%e3%82%a2%e3%82%a4%e3%82%ad%e3%83%a3%e3%83%83%e3%83%81%e7%94%bb%e5%83%8f%e3%82%92%e8%a8%ad%e5%ae%9a%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/blue-bear.jp\/kb\/wordpress-%e7%94%bb%e5%83%8furl%e3%82%92%e6%8c%87%e5%ae%9a%e3%81%97%e3%81%a6%e3%82%a2%e3%82%a4%e3%82%ad%e3%83%a3%e3%83%83%e3%83%81%e7%94%bb%e5%83%8f%e3%82%92%e8%a8%ad%e5%ae%9a%e3%81%99%e3%82%8b\/","title":{"rendered":"[wordpress] \u753b\u50cfURL\u3092\u6307\u5b9a\u3057\u3066\u30a2\u30a4\u30ad\u30e3\u30c3\u30c1\u753b\u50cf\u3092\u8a2d\u5b9a\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"\n<p>wordpress\u306e\u30a2\u30a4\u30ad\u30e3\u30c3\u30c1\u753b\u50cf\u3092\u8a2d\u5b9a\u3059\u308b\u306b\u306f\u3001WebUI\u304b\u3089\u3060\u3051\u3067\u306a\u304fwp_insert_attachment\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u30d7\u30ed\u30b0\u30e9\u30e0\u7d4c\u7531\u3067\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u304c\u3001wp_insert_attachment\u306f\u30ed\u30fc\u30ab\u30eb\u306b\u304a\u3044\u3066\u3042\u308b\u753b\u50cf\u306e\u307f\u6307\u5b9a\u3067\u304d\u308b\u305f\u3081\u3001\u901a\u5e38\u3067\u306f\u30a4\u30f3\u30bf\u30fc\u30cd\u30c3\u30c8\u4e0a\u306b\u3042\u308b\u753b\u50cf\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u306a\u3044\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u306e\u65b9\u6cd5\u3067\u30a4\u30f3\u30bf\u30fc\u30cd\u30c3\u30c8\u4e0a\u306b\u3042\u308b\u753b\u50cf\u3092\u30a2\u30a4\u30ad\u30e3\u30c3\u30c1\u753b\u50cf\u3068\u3057\u3066\u767b\u9332\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd<\/p>\n\n\n\n<p>func_uploadImage_from_url\u3068\u3044\u3046\u95a2\u6570\u3092\u4f5c\u6210\u3057\u3066\u307f\u305f<\/p>\n\n\n\n<p>\u4f7f\u3044\u65b9\u3068\u3057\u3066\u306f<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$post_id = \"1\";\n$image_url = \"https:\/\/www.example.com\/image\/main.png\"\nfunc_uploadImage_from_url($post_id,$image_url);<\/pre>\n\n\n\n<p>\u4e00\u65e6\u30ed\u30fc\u30ab\u30eb\u306e\u30e1\u30c7\u30a3\u30a2\u30a4\u30e1\u30fc\u30b8\u306b\u4fdd\u5b58\u3057\u3066\u304b\u3089\u30a2\u30a4\u30ad\u30e3\u30c3\u30c1\u753b\u50cf\u3068\u3057\u3066\u767b\u9332\u3057\u3066\u3044\u308b<\/p>\n\n\n\n<pre class=\"wp-block-verse\">function func_uploadImage_from_url($post_id,$image_url){\n\n\/\/ Add Featured Image to Post\n$image_name = basename($image_url);\n$upload_dir = wp_upload_dir(); \/\/ Set upload folder\n$image_data = file_get_contents($image_url); \/\/ Get image data\n$unique_file_name = wp_unique_filename( $upload_dir['path'], $image_name ); \/\/ Generate unique name\n$filename = basename( $unique_file_name ); \/\/ Create image file name\n\n\/\/ Check folder permission and define file location\nif( wp_mkdir_p( $upload_dir['path'] ) ) {\n$file = $upload_dir['path'] . '\/' . $filename;\n} else {\n$file = $upload_dir['basedir'] . '\/' . $filename;\n}\n\n\/\/ Create the image file on the server\nfile_put_contents( $file, $image_data );\n\n\/\/ Check image file type\n$wp_filetype = wp_check_filetype( $filename, null );\n\n\/\/ Set attachment data\n$attachment = array(\n'post_mime_type' => $wp_filetype['type'],\n'post_title' => sanitize_file_name( $filename ),\n'post_content' => '',\n'post_status' => 'inherit'\n);\n\n\/\/ Create the attachment\n$attach_id = wp_insert_attachment( $attachment, $file, $post_id );\n\n\/\/ Include image.php\nrequire_once(ABSPATH . 'wp-admin\/includes\/image.php');\n\n\/\/ Define attachment metadata\n$attach_data = wp_generate_attachment_metadata( $attach_id, $file );\n\n\/\/ Assign metadata to attachment\nwp_update_attachment_metadata( $attach_id, $attach_data );\n\n\/\/ And finally assign featured image to post\nset_post_thumbnail( $post_id, $attach_id );\n}<\/pre>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/41524931\/how-to-set-featured-image-programmatically-from-url\" target=\"_blank\" rel=\"noreferrer noopener\">\u53c2\u8003\uff1ahttps:\/\/stackoverflow.com\/questions\/41524931\/how-to-set-featured-image-programmatically-from-url<\/a><\/p>\n\n\n","protected":false},"excerpt":{"rendered":"<p>wordpress\u306e\u30a2\u30a4\u30ad\u30e3\u30c3\u30c1\u753b\u50cf\u3092\u8a2d<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[341],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/3090"}],"collection":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/comments?post=3090"}],"version-history":[{"count":2,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/3090\/revisions"}],"predecessor-version":[{"id":3092,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/3090\/revisions\/3092"}],"wp:attachment":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/media?parent=3090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/categories?post=3090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/tags?post=3090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}