{"id":32814,"date":"2024-11-25T15:30:08","date_gmt":"2024-11-25T07:30:08","guid":{"rendered":"https:\/\/fwq.ai\/blog\/32814\/"},"modified":"2024-11-25T15:30:08","modified_gmt":"2024-11-25T07:30:08","slug":"%e5%a6%82%e4%bd%95%e4%b8%bawordpress%e6%8f%92%e4%bb%b6%e6%b7%bb%e5%8a%a0%e7%94%a8%e6%88%b7%e7%99%bb%e5%bd%95%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/32814\/","title":{"rendered":"\u5982\u4f55\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u7528\u6237\u767b\u5f55\u529f\u80fd"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169389776719929.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u7528\u6237\u767b\u5f55\u529f\u80fd\u63d2\u56fe\" alt=\"\u5982\u4f55\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u7528\u6237\u767b\u5f55\u529f\u80fd\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u7528\u6237\u767b\u5f55\u529f\u80fd<\/p>\n<p>\u5728\u5f00\u53d1WordPress\u63d2\u4ef6\u65f6\uff0c\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u4e3a\u63d2\u4ef6\u6dfb\u52a0\u7528\u6237\u767b\u5f55\u7684\u529f\u80fd\uff0c\u4ee5\u4fbf\u4e8e\u5bf9\u7279\u5b9a\u7528\u6237\u8fdb\u884c\u6743\u9650\u7ba1\u7406\u3001\u6570\u636e\u4fdd\u5b58\u7b49\u64cd\u4f5c\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u7528\u6237\u767b\u5f55\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u4e86\u89e3\u4e00\u4e9bWordPress\u7684\u7528\u6237\u767b\u5f55\u76f8\u5173\u7684\u51fd\u6570\u548c\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u7684\u5f00\u53d1\u3002<\/p>\n<ol>\n<li>wp_login_form()\uff1a\u8f93\u51fa\u4e00\u4e2a\u7b80\u5355\u7684\u767b\u5f55\u8868\u5355\uff0c\u7528\u4e8e\u5728\u524d\u53f0\u663e\u793a\u7528\u6237\u767b\u5f55\u754c\u9762\u3002<\/li>\n<li>wp_login_url()\uff1a\u83b7\u53d6\u7528\u6237\u767b\u5f55\u7684URL\u5730\u5740\uff0c\u53ef\u4ee5\u7528\u4e8e\u5728\u63d2\u4ef6\u4e2d\u751f\u6210\u767b\u5f55\u94fe\u63a5\u3002<\/li>\n<li>is_user_logged_in()\uff1a\u5224\u65ad\u5f53\u524d\u7528\u6237\u662f\u5426\u5df2\u767b\u5f55\u3002<\/li>\n<li>wp_logout()\uff1a\u767b\u51fa\u5f53\u524d\u767b\u5f55\u7528\u6237\u3002<\/li>\n<li>wp_create_user()\uff1a\u521b\u5efa\u65b0\u7528\u6237\u3002<\/li>\n<li>wp_set_auth_cookie()\uff1a\u8bbe\u7f6e\u7528\u6237\u7684\u8ba4\u8bc1Cookie\u3002<\/li>\n<li>wp_redirect()\uff1a\u8fdb\u884c\u9875\u9762\u8df3\u8f6c\u3002<\/li>\n<\/ol>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u901a\u8fc7\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u6765\u5b9e\u73b0\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u7528\u6237\u767b\u5f55\u529f\u80fd\u3002<\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u5728\u63d2\u4ef6\u7684\u4e3b\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/li>\n<\/ol>\n<pre>\/*\nPlugin Name: My Plugin\n*\/\n\n\/\/ \u6dfb\u52a0\u767b\u5f55\u94fe\u63a5\nfunction my_plugin_menu() {\n    add_menu_page('My Plugin', 'My Plugin', 'edit_posts', 'my-plugin', 'my_plugin_page');\n}\nadd_action('admin_menu', 'my_plugin_menu');\n\n\/\/ \u767b\u5f55\u9875\u9762\nfunction my_plugin_page() {\n    if(!is_user_logged_in()) {\n        echo '&lt;h2&gt;Please &lt;a href=\"'%20.%20wp_login_url()%20.%20'\"&gt;log in&lt;\/a&gt; to use this plugin.&lt;\/h2&gt;';\n    } else {\n        \/\/ \u63d2\u4ef6\u529f\u80fd\u4ee3\u7801\n    }\n}\n\n\/\/ \u6ce8\u518c\u7528\u6237\u767b\u5f55\u6210\u529f\u540e\u7684\u56de\u8c03\u51fd\u6570\nfunction my_login_redirect($redirect_to, $request, $user) {\n    return admin_url('admin.php?page=my-plugin');\n}\nadd_filter('login_redirect', 'my_login_redirect', 10, 3);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u901a\u8fc7add_menu_page()\u51fd\u6570\u6dfb\u52a0\u4e86\u4e00\u4e2a\u83dc\u5355\u9875\u9762\uff0c\u53ea\u6709\u5177\u5907edit_posts\u6743\u9650\u7684\u7528\u6237\u624d\u80fd\u8bbf\u95ee\u3002\u5728my_plugin_page()\u51fd\u6570\u4e2d\uff0c\u901a\u8fc7\u5224\u65adis_user_logged_in()\u51fd\u6570\u6765\u5224\u65ad\u7528\u6237\u662f\u5426\u5df2\u767b\u5f55\u3002\u5982\u679c\u7528\u6237\u672a\u767b\u5f55\uff0c\u5219\u663e\u793a\u767b\u5f55\u94fe\u63a5\uff0c\u70b9\u51fb\u94fe\u63a5\u540e\u5c06\u8df3\u8f6c\u5230WordPress\u9ed8\u8ba4\u7684\u767b\u5f55\u9875\u9762\u3002\u5982\u679c\u7528\u6237\u5df2\u767b\u5f55\uff0c\u5219\u663e\u793a\u63d2\u4ef6\u529f\u80fd\u4ee3\u7801\u3002<\/li>\n<li>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u8fd8\u901a\u8fc7add_filter()\u51fd\u6570\u6ce8\u518c\u4e86\u4e00\u4e2a\u56de\u8c03\u51fd\u6570my_login_redirect()\uff0c\u7528\u4e8e\u7528\u6237\u767b\u5f55\u6210\u529f\u540e\u7684\u8df3\u8f6c\u3002\u5728\u8be5\u56de\u8c03\u51fd\u6570\u4e2d\uff0c\u901a\u8fc7\u8fd4\u56deadmin_url(&#8216;admin.php?page=my-plugin&#8217;)\u6765\u6307\u5b9a\u4e86\u7528\u6237\u767b\u5f55\u6210\u529f\u540e\u8981\u8df3\u8f6c\u7684\u9875\u9762\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u5c31\u5b8c\u6210\u4e86\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u7528\u6237\u767b\u5f55\u529f\u80fd\u7684\u5f00\u53d1\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u63d2\u4ef6\u7684\u5177\u4f53\u9700\u6c42\uff0c\u5728my_plugin_page()\u51fd\u6570\u4e2d\u6dfb\u52a0\u76f8\u5e94\u7684\u63d2\u4ef6\u529f\u80fd\u4ee3\u7801\u3002<\/p>\n<p>\u5e0c\u671b\u672c\u6587\u80fd\u5bf9\u4f60\u7684WordPress\u63d2\u4ef6\u5f00\u53d1\u5de5\u4f5c\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u7528\u6237\u767b\u5f55\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\u7528\u6237\u767b\u5f55\u529f\u80fd \u5728\u5f00\u53d1WordPress\u63d2\u4ef6\u65f6\uff0c\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u4e3a\u63d2\u4ef6\u6dfb\u52a0\u7528\u6237\u767b\u5f55\u7684\u529f\u80fd\uff0c\u4ee5\u4fbf\u4e8e\u5bf9\u7279\u5b9a\u7528\u6237\u8fdb\u884c\u6743\u9650\u7ba1\u7406\u3001\u6570\u636e\u4fdd\u5b58\u7b49\u64cd\u4f5c\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u7528\u6237\u767b\u5f55\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u4e86\u89e3\u4e00\u4e9bWordPress\u7684\u7528\u6237\u767b\u5f55\u76f8\u5173\u7684\u51fd\u6570\u548c\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u7684\u5f00\u53d1\u3002 wp_login_form()\uff1a\u8f93\u51fa\u4e00\u4e2a\u7b80\u5355\u7684\u767b\u5f55\u8868\u5355\uff0c\u7528\u4e8e\u5728\u524d\u53f0\u663e\u793a\u7528\u6237\u767b\u5f55\u754c\u9762\u3002 wp_login_url()\uff1a\u83b7\u53d6\u7528\u6237\u767b\u5f55\u7684URL\u5730\u5740\uff0c\u53ef\u4ee5\u7528\u4e8e\u5728\u63d2\u4ef6\u4e2d\u751f\u6210\u767b\u5f55\u94fe\u63a5\u3002 is_user_logged_in()\uff1a\u5224\u65ad\u5f53\u524d\u7528\u6237\u662f\u5426\u5df2\u767b\u5f55\u3002 wp_logout()\uff1a\u767b\u51fa\u5f53\u524d\u767b\u5f55\u7528\u6237\u3002 wp_create_user()\uff1a\u521b\u5efa\u65b0\u7528\u6237\u3002 wp_set_auth_cookie()\uff1a\u8bbe\u7f6e\u7528\u6237\u7684\u8ba4\u8bc1Cookie\u3002 wp_redirect()\uff1a\u8fdb\u884c\u9875\u9762\u8df3\u8f6c\u3002 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u901a\u8fc7\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u6765\u5b9e\u73b0\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u7528\u6237\u767b\u5f55\u529f\u80fd\u3002 \u9996\u5148\uff0c\u5728\u63d2\u4ef6\u7684\u4e3b\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a \/* Plugin Name: My Plugin *\/ \/\/ \u6dfb\u52a0\u767b\u5f55\u94fe\u63a5 function my_plugin_menu() { add_menu_page(&#8216;My Plugin&#8217;, &#8216;My Plugin&#8217;, &#8216;edit_posts&#8217;, &#8216;my-plugin&#8217;, &#8216;my_plugin_page&#8217;); } add_action(&#8216;admin_menu&#8217;, &#8216;my_plugin_menu&#8217;); \/\/ \u767b\u5f55\u9875\u9762 function my_plugin_page() { if(!is_user_logged_in()) { echo &#8216;&lt;h2&gt;Please &lt;a href=&#8221;&#8216;%20.%20wp_login_url()%20.%20&#8242;&#8221;&gt;log in&lt;\/a&gt; to use this plugin.&lt;\/h2&gt;&#8217;; } else { \/\/ \u63d2\u4ef6\u529f\u80fd\u4ee3\u7801 } [&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-32814","post","type-post","status-publish","format-standard","hentry","category-cms"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32814","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=32814"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32814\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=32814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=32814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=32814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}