{"id":32749,"date":"2024-11-25T11:59:47","date_gmt":"2024-11-25T03:59:47","guid":{"rendered":"https:\/\/fwq.ai\/blog\/32749\/"},"modified":"2024-11-25T11:59:47","modified_gmt":"2024-11-25T03:59:47","slug":"%e5%a6%82%e4%bd%95%e4%b8%bawordpress%e6%8f%92%e4%bb%b6%e6%b7%bb%e5%8a%a0%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/32749\/","title":{"rendered":"\u5982\u4f55\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/465\/014\/169387948461614.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd\u63d2\u56fe\" alt=\"\u5982\u4f55\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>WordPress\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u5f00\u6e90\u5185\u5bb9\u7ba1\u7406\u7cfb\u7edf\uff0c\u5b83\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u4f7f\u5f97\u8bb8\u591a\u7f51\u7ad9\u5f00\u53d1\u4eba\u5458\u9009\u62e9\u4f7f\u7528\u5b83\u3002\u5176\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u7279\u70b9\u662f\u80fd\u591f\u901a\u8fc7\u63d2\u4ef6\u6765\u6269\u5c55WordPress\u7684\u529f\u80fd\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684WordPress\u63d2\u4ef6<br \/>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684WordPress\u63d2\u4ef6\u3002\u5728WordPress\u7684\u63d2\u4ef6\u76ee\u5f55\uff08wp-content\/plugins\/\uff09\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u5939\uff0c\u547d\u540d\u4e3a&#8221;my-plugin&#8221;\uff08\u6216\u8005\u6839\u636e\u4f60\u7684\u9700\u6c42\u547d\u540d\uff09\u3002\u5728\u8be5\u6587\u4ef6\u5939\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a&#8221;my-plugin.php&#8221;\u7684PHP\u6587\u4ef6\uff0c\u5e76\u5728\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/li>\n<\/ol>\n<pre>&lt;?php \/*\nPlugin Name: My Plugin\n*\/\n\n\/\/ \u6dfb\u52a0\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd\u7684\u4ee3\u7801\u5c06\u5728\u6b64\u5904\u6dfb\u52a0\n?&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u5728\u63d2\u4ef6\u8bbe\u7f6e\u9875\u9762\u4e2d\u6dfb\u52a0\u6587\u4ef6\u4e0a\u4f20\u8868\u5355<br \/>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5728\u63d2\u4ef6\u8bbe\u7f6e\u9875\u9762\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u6587\u4ef6\u4e0a\u4f20\u8868\u5355\u3002\u5728&#8221;my-plugin.php&#8221;\u6587\u4ef6\u4e2d\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/li>\n<\/ol>\n<pre>&lt;?php function my_plugin_admin_menu() {\n    add_options_page('My Plugin Settings', 'My Plugin', 'manage_options', 'my-plugin', 'my_plugin_settings_page');\n}\nadd_action('admin_menu', 'my_plugin_admin_menu');\n\nfunction my_plugin_settings_page() {\n    ?&gt;&lt;div class=\"wrap\"&gt;\n        &lt;h2&gt;My Plugin Settings&lt;\/h2&gt;\n        &lt;form method=\"post\" enctype=\"multipart\/form-data\"&gt;\n            &lt;input type=\"file\" name=\"my_file\"&gt;&lt;input type=\"submit\" value=\"Upload\"&gt;\n&lt;\/form&gt;\n    &lt;\/div&gt;\n    &lt;?php }\n?&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528add_options_page\u51fd\u6570\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u9009\u9879\u5361\u5230WordPress\u7684\u8bbe\u7f6e\u9875\u9762\uff0c\u7136\u540e\u4f7f\u7528my_plugin_settings_page\u51fd\u6570\u5728\u9009\u9879\u5361\u9875\u9762\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u8868\u5355\u3002\u8868\u5355\u5305\u542b\u4e00\u4e2a\u6587\u4ef6\u9009\u62e9\u8f93\u5165\u6846\u548c\u4e00\u4e2a\u4e0a\u4f20\u6309\u94ae\u3002<\/p>\n<ol>\n<li>\u5904\u7406\u6587\u4ef6\u4e0a\u4f20\u5e76\u4fdd\u5b58\u6587\u4ef6<br \/>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u7f16\u5199\u4ee3\u7801\u6765\u5904\u7406\u6587\u4ef6\u4e0a\u4f20\u5e76\u4fdd\u5b58\u6587\u4ef6\u3002\u5728&#8221;my-plugin.php&#8221;\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/li>\n<\/ol>\n<pre>&lt;?php function my_plugin_save_file() {\n    if (isset($_FILES['my_file'])) {\n        $upload_dir = wp_upload_dir();\n        $file_name = $_FILES['my_file']['name'];\n        $file_temp = $_FILES['my_file']['tmp_name'];\n        $file_path = $upload_dir['path'] . '\/' . $file_name;\n\n        if (move_uploaded_file($file_temp, $file_path)) {\n            echo 'File uploaded successfully.';\n        } else {\n            echo 'Failed to upload file.';\n        }\n    }\n}\nadd_action('admin_init', 'my_plugin_save_file');\n?&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u68c0\u67e5\u662f\u5426\u6709\u6587\u4ef6\u4e0a\u4f20\u3002\u7136\u540e\uff0c\u4f7f\u7528wp_upload_dir\u51fd\u6570\u83b7\u53d6WordPress\u4e0a\u4f20\u76ee\u5f55\u7684\u8def\u5f84\uff0c\u4f7f\u7528move_uploaded_file\u51fd\u6570\u5c06\u6587\u4ef6\u4ece\u4e34\u65f6\u4f4d\u7f6e\u79fb\u52a8\u5230\u4e0a\u4f20\u76ee\u5f55\u4e2d\u3002<\/p>\n<ol>\n<li>\u663e\u793a\u4e0a\u4f20\u7684\u6587\u4ef6\u4fe1\u606f<br \/>\u6700\u540e\uff0c\u6211\u4eec\u9700\u8981\u5728\u63d2\u4ef6\u8bbe\u7f6e\u9875\u9762\u4e2d\u663e\u793a\u4e0a\u4f20\u7684\u6587\u4ef6\u4fe1\u606f\u3002\u5728&#8221;my-plugin.php&#8221;\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/li>\n<\/ol>\n<pre>&lt;?php function my_plugin_show_file_info() {\n    $upload_dir = wp_upload_dir();\n    $file_path = $upload_dir['path'] . '\/' . $_FILES['my_file']['name'];\n\n    if (file_exists($file_path)) {\n        $file_size = filesize($file_path);\n        $file_type = wp_check_filetype(basename($file_path), null);\n        $file_url = $upload_dir['url'] . '\/' . $_FILES['my_file']['name'];\n\n        echo '&lt;p&gt;File URL: &lt;a href=\"'%20.%20%24file_url%20.%20'\"&gt;' . $file_url . '&lt;\/a&gt;';\n        echo '&lt;p&gt;File Type: ' . $file_type['type'] . '&lt;\/p&gt;';\n        echo '&lt;p&gt;File Size: ' . $file_size . ' bytes&lt;\/p&gt;';\n    }\n}\nadd_action('admin_notices', 'my_plugin_show_file_info');\n?&amp;gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u83b7\u53d6\u4e0a\u4f20\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u7136\u540e\u4f7f\u7528file_exists\u51fd\u6570\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u6211\u4eec\u4f7f\u7528wp_check_filetype\u51fd\u6570\u83b7\u53d6\u6587\u4ef6\u7684\u7c7b\u578b\uff0c\u5e76\u5c06\u6587\u4ef6\u7684URL\u3001\u7c7b\u578b\u548c\u5927\u5c0f\u663e\u793a\u5728\u63d2\u4ef6\u8bbe\u7f6e\u9875\u9762\u4e0a\u3002<\/p>\n<p>\u603b\u7ed3\uff1a<br \/>\u901a\u8fc7\u672c\u6587\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u5982\u4f55\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd\u3002\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684WordPress\u63d2\u4ef6\uff0c\u5e76\u5728\u63d2\u4ef6\u8bbe\u7f6e\u9875\u9762\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u6587\u4ef6\u4e0a\u4f20\u8868\u5355\u3002\u7136\u540e\uff0c\u6211\u4eec\u7f16\u5199\u4e86\u4ee3\u7801\u6765\u5904\u7406\u6587\u4ef6\u4e0a\u4f20\u5e76\u4fdd\u5b58\u6587\u4ef6\uff0c\u6700\u540e\u5728\u63d2\u4ef6\u8bbe\u7f6e\u9875\u9762\u4e2d\u663e\u793a\u4e86\u4e0a\u4f20\u6587\u4ef6\u7684\u4fe1\u606f\u3002\u8fd9\u4e2a\u4f8b\u5b50\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u5982\u4f55\u6269\u5c55WordPress\u63d2\u4ef6\u7684\u529f\u80fd\uff0c\u540c\u65f6\u63d0\u9ad8\u81ea\u5df1\u7684\u5f00\u53d1\u6280\u80fd\u3002<\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u793a\u4f8b\u53ea\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u6f14\u793a\uff0c\u5b9e\u9645\u5f00\u53d1\u4e2d\u53ef\u80fd\u8fd8\u9700\u8981\u6dfb\u52a0\u66f4\u591a\u7684\u903b\u8f91\u548c\u6821\u9a8c\uff0c\u4ee5\u4fdd\u8bc1\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd\u7684\u5b89\u5168\u6027\u548c\u53ef\u9760\u6027\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u5728\u5f00\u53d1WordPress\u63d2\u4ef6\u65f6\u589e\u52a0\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd \u5f15\u8a00\uff1aWordPress\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u5f00\u6e90\u5185\u5bb9\u7ba1\u7406\u7cfb\u7edf\uff0c\u5b83\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u4f7f\u5f97\u8bb8\u591a\u7f51\u7ad9\u5f00\u53d1\u4eba\u5458\u9009\u62e9\u4f7f\u7528\u5b83\u3002\u5176\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u7279\u70b9\u662f\u80fd\u591f\u901a\u8fc7\u63d2\u4ef6\u6765\u6269\u5c55WordPress\u7684\u529f\u80fd\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u521b\u5efa\u4e00\u4e2a\u65b0\u7684WordPress\u63d2\u4ef6\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684WordPress\u63d2\u4ef6\u3002\u5728WordPress\u7684\u63d2\u4ef6\u76ee\u5f55\uff08wp-content\/plugins\/\uff09\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u5939\uff0c\u547d\u540d\u4e3a&#8221;my-plugin&#8221;\uff08\u6216\u8005\u6839\u636e\u4f60\u7684\u9700\u6c42\u547d\u540d\uff09\u3002\u5728\u8be5\u6587\u4ef6\u5939\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a&#8221;my-plugin.php&#8221;\u7684PHP\u6587\u4ef6\uff0c\u5e76\u5728\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a &lt;?php \/* Plugin Name: My Plugin *\/ \/\/ \u6dfb\u52a0\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd\u7684\u4ee3\u7801\u5c06\u5728\u6b64\u5904\u6dfb\u52a0 ?&gt; \u767b\u5f55\u540e\u590d\u5236 \u5728\u63d2\u4ef6\u8bbe\u7f6e\u9875\u9762\u4e2d\u6dfb\u52a0\u6587\u4ef6\u4e0a\u4f20\u8868\u5355\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5728\u63d2\u4ef6\u8bbe\u7f6e\u9875\u9762\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u6587\u4ef6\u4e0a\u4f20\u8868\u5355\u3002\u5728&#8221;my-plugin.php&#8221;\u6587\u4ef6\u4e2d\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a &lt;?php function my_plugin_admin_menu() { add_options_page(&#8216;My Plugin Settings&#8217;, &#8216;My Plugin&#8217;, &#8216;manage_options&#8217;, &#8216;my-plugin&#8217;, &#8216;my_plugin_settings_page&#8217;); } add_action(&#8216;admin_menu&#8217;, &#8216;my_plugin_admin_menu&#8217;); function my_plugin_settings_page() { ?&gt;&lt;div class=&#8221;wrap&#8221;&gt; &lt;h2&gt;My Plugin Settings&lt;\/h2&gt; &lt;form method=&#8221;post&#8221; enctype=&#8221;multipart\/form-data&#8221;&gt; &lt;input type=&#8221;file&#8221; name=&#8221;my_file&#8221;&gt;&lt;input type=&#8221;submit&#8221; value=&#8221;Upload&#8221;&gt; &lt;\/form&gt; &lt;\/div&gt; &lt;?php } ?&gt; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528add_options_page\u51fd\u6570\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u9009\u9879\u5361\u5230WordPress\u7684\u8bbe\u7f6e\u9875\u9762\uff0c\u7136\u540e\u4f7f\u7528my_plugin_settings_page\u51fd\u6570\u5728\u9009\u9879\u5361\u9875\u9762\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u8868\u5355\u3002\u8868\u5355\u5305\u542b\u4e00\u4e2a\u6587\u4ef6\u9009\u62e9\u8f93\u5165\u6846\u548c\u4e00\u4e2a\u4e0a\u4f20\u6309\u94ae\u3002 \u5904\u7406\u6587\u4ef6\u4e0a\u4f20\u5e76\u4fdd\u5b58\u6587\u4ef6\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u7f16\u5199\u4ee3\u7801\u6765\u5904\u7406\u6587\u4ef6\u4e0a\u4f20\u5e76\u4fdd\u5b58\u6587\u4ef6\u3002\u5728&#8221;my-plugin.php&#8221;\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a &lt;?php [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-32749","post","type-post","status-publish","format-standard","hentry","category-cms"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32749","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=32749"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32749\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=32749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=32749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=32749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}