{"id":45041,"date":"2024-06-03T00:12:02","date_gmt":"2024-06-02T16:12:02","guid":{"rendered":"http:\/\/www.biocloudservice.com\/wordpress\/?p=45041"},"modified":"2024-06-03T00:12:02","modified_gmt":"2024-06-02T16:12:02","slug":"%e4%b8%80%e4%b8%aa%e8%83%bd%e5%a4%9f%e4%b8%8a%e4%bc%a0%e6%9c%ac%e5%9c%b0%e5%9b%be%e7%89%87%e7%9a%84%e6%8f%92%e4%bb%b6%e7%a4%ba%e4%be%8b","status":"publish","type":"post","link":"http:\/\/www.biocloudservice.com\/wordpress\/?p=45041","title":{"rendered":"\u4e00\u4e2a\u80fd\u591f\u4e0a\u4f20\u672c\u5730\u56fe\u7247\u7684\u63d2\u4ef6\u793a\u4f8b"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\/*\r\nPlugin Name: a Hello World with Image Post\r\nDescription: Creates a post titled \"\u4f60\u597d\" with a local image as content upon activation.\r\nVersion: 1.0\r\nAuthor: Your Name\r\nAuthor URI: http:\/\/yourwebsite.com\r\nLicense: GPL2\r\n*\/\r\n\r\n\/\/ Exit if accessed directly\r\nif (!defined('ABSPATH')) {\r\n    exit;\r\n}\r\n\r\n\/\/ Hook into activation to create the post and upload the image\r\nregister_activation_hook(__FILE__, 'create_hello_world_post_with_image');\r\n\r\nfunction create_hello_world_post_with_image() {\r\n    \/\/ Start output buffering to catch any output\r\n    ob_start();\r\n\r\n    \/\/ Define the local path to the image\r\n    $local_image_path = plugin_dir_path(__FILE__) . '1.jpg'; \/\/ Replace with your image's local path\r\n\r\n    \/\/ Check if the image file exists\r\n    if (!file_exists($local_image_path)) {\r\n        error_log('Hello World with Image Post: Image file not found.');\r\n        \/\/ End output buffering and discard any output\r\n        ob_end_clean();\r\n        return;\r\n    }\r\n\r\n    \/\/ Read the image into a variable\r\n    $image_content = file_get_contents($local_image_path);\r\n\r\n    \/\/ Create an array that looks like a $_FILES array\r\n    $image_array = array(\r\n        'name' => basename($local_image_path),\r\n        'type' => 'image\/jpeg', \/\/ Replace with your image's mime type\r\n        'tmp_name' => $local_image_path,\r\n        'error' => 0,\r\n        'size' => filesize($local_image_path)\r\n    );\r\n\r\n    \/\/ Use the media_handle_sideload function to upload the image\r\n    $attachment_id = media_handle_sideload($image_array, 0);\r\n\r\n    \/\/ Check for errors and that the attachment was created successfully\r\n    if (is_wp_error($attachment_id)) {\r\n        error_log('Hello World with Image Post: ' . $attachment_id->get_error_message());\r\n        \/\/ End output buffering and discard any output\r\n        ob_end_clean();\r\n        return;\r\n    }\r\n\r\n    \/\/ Create the post\r\n    $post = array(\r\n        'post_title'  => '\u4f60\u597d',\r\n        'post_content' => '',\r\n        'post_status'  => 'publish',\r\n        'post_author' => 1, \/\/ Set the post author ID\r\n        'post_type'   => 'post',\r\n    );\r\n\r\n    \/\/ Insert the post into the database\r\n    $post_id = wp_insert_post($post);\r\n\r\n    \/\/ Check if the post was created successfully\r\n    if (is_wp_error($post_id)) {\r\n        error_log('Hello World with Image Post: Failed to create post.');\r\n        \/\/ End output buffering and discard any output\r\n        ob_end_clean();\r\n        return;\r\n    }\r\n\r\n    \/\/ Update the post content with the image\r\n    $post_content = wp_get_attachment_image($attachment_id, 'thumbnail'); \/\/ Use the thumbnail size\r\n    $updated_post = array(\r\n        'ID'           => $post_id,\r\n        'post_content' => $post_content,\r\n    );\r\n    wp_update_post($updated_post);\r\n\r\n    \/\/ End output buffering and discard any output\r\n    ob_end_clean();\r\n}\r\n\r\n?><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":""},"categories":[1],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/45041"}],"collection":[{"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=45041"}],"version-history":[{"count":1,"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/45041\/revisions"}],"predecessor-version":[{"id":45042,"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/45041\/revisions\/45042"}],"wp:attachment":[{"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=45041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=45041"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=45041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}