{"id":4319,"date":"2024-10-29T11:45:34","date_gmt":"2024-10-29T03:45:34","guid":{"rendered":"https:\/\/www.openbyt.com\/?p=4319"},"modified":"2024-10-29T11:45:36","modified_gmt":"2024-10-29T03:45:36","slug":"why-is-rest_api_init-not-working-in-my-custom-wordpress-plugin","status":"publish","type":"post","link":"https:\/\/www.openbyt.com\/zh\/\u70ba\u4ec0\u9ebc-rest_api_init-\u5728\u6211\u7684\u81ea\u8a02-wordpress-\u5916\u639b\u4e2d\u7121\u6cd5\u904b\u4f5c\uff1f","title":{"rendered":"\u70ba\u4f55\u6211\u7684\u81ea\u8a02 WordPress \u5916\u639b\u7a0b\u5f0f\u4e2d rest_api_init \u7121\u6cd5\u904b\u4f5c\uff1f"},"content":{"rendered":"<p>\u9047\u5230\u4ee5\u4e0b\u554f\u984c <code>rest_api_init<\/code> \u5728\u60a8\u7684\u81ea\u8a02 WordPress \u5916\u639b\u4e2d\u7121\u6cd5\u904b\u4f5c\u53ef\u80fd\u6703\u76f8\u7576\u4ee4\u4eba\u6cae\u55aa\uff0c\u5c24\u5176\u662f\u7576\u770b\u8d77\u4f86\u4e00\u5207\u90fd\u61c9\u8a72\u914d\u7f6e\u6b63\u78ba\u6642\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u63a2\u8a0e\u4ee5\u4e0b\u53ef\u80fd\u7684\u539f\u56e0 <code>rest_api_init<\/code> \u53ef\u80fd\u7121\u6cd5\u904b\u4f5c\u3001\u6aa2\u8996\u5e38\u898b\u7684\u9677\u9631\uff0c\u4e26\u8a0e\u8ad6\u6700\u4f73\u5be6\u52d9\uff0c\u4ee5\u78ba\u4fdd\u60a8\u7684\u81ea\u8a02 REST API \u8def\u7531\u80fd\u9806\u66a2\u904b\u4f5c\u3002\u9019\u662f\u5c08\u70ba\u5e0c\u671b\u5728 WordPress \u4e2d\u5efa\u7acb\u5f37\u5927\u81ea\u8a02 API \u7684\u958b\u767c\u4eba\u54e1\u6240\u8a2d\u8a08\u7684\u6280\u8853\u6df1\u7a76\u3002<\/p><figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.26.34-A-visually-appealing-and-modern-illustration-explaining-the-troubleshooting-process-for-rest_api_init-in-a-custom-WordPress-plugin.-Show-a-well-orga.webp\" alt=\"\" class=\"wp-image-4322\" srcset=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.26.34-A-visually-appealing-and-modern-illustration-explaining-the-troubleshooting-process-for-rest_api_init-in-a-custom-WordPress-plugin.-Show-a-well-orga.webp 1024w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.26.34-A-visually-appealing-and-modern-illustration-explaining-the-troubleshooting-process-for-rest_api_init-in-a-custom-WordPress-plugin.-Show-a-well-orga-600x600.webp 600w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.26.34-A-visually-appealing-and-modern-illustration-explaining-the-troubleshooting-process-for-rest_api_init-in-a-custom-WordPress-plugin.-Show-a-well-orga-100x100.webp 100w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.26.34-A-visually-appealing-and-modern-illustration-explaining-the-troubleshooting-process-for-rest_api_init-in-a-custom-WordPress-plugin.-Show-a-well-orga-300x300.webp 300w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.26.34-A-visually-appealing-and-modern-illustration-explaining-the-troubleshooting-process-for-rest_api_init-in-a-custom-WordPress-plugin.-Show-a-well-orga-150x150.webp 150w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.26.34-A-visually-appealing-and-modern-illustration-explaining-the-troubleshooting-process-for-rest_api_init-in-a-custom-WordPress-plugin.-Show-a-well-orga-768x768.webp 768w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.26.34-A-visually-appealing-and-modern-illustration-explaining-the-troubleshooting-process-for-rest_api_init-in-a-custom-WordPress-plugin.-Show-a-well-orga-12x12.webp 12w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.26.34-A-visually-appealing-and-modern-illustration-explaining-the-troubleshooting-process-for-rest_api_init-in-a-custom-WordPress-plugin.-Show-a-well-orga-96x96.webp 96w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><h3 class=\"wp-block-heading\">\u77ad\u89e3 rest_api_init \u53ca\u5176\u5728 WordPress \u4e2d\u7684\u4f5c\u7528<\/h3><p>\u7684 <code>rest_api_init<\/code> action \u9264\u5b50\u662f\u7528\u4f86\u65b0\u589e\u81ea\u8a02\u8def\u7531\u548c\u7aef\u9ede\u5230 WordPress REST API\u3002\u5b83\u5728 REST API \u4f3a\u670d\u5668\u521d\u59cb\u5316\u6642\u57f7\u884c\uff0c\u901a\u5e38\u7528\u4f86\u8a3b\u518a\u65b0\u7684 REST API \u8def\u7531\u3002\u5982\u679c\u60a8\u7684\u81ea\u8a02\u7aef\u9ede\u7121\u6cd5\u5982\u9810\u671f\u822c\u904b\u4f5c\uff0c\u901a\u5e38\u662f\u7531\u65bc\u932f\u8aa4\u8a2d\u5b9a\u6216\u4e0d\u7576\u4f7f\u7528 <code>rest_api_init<\/code>.\u77ad\u89e3\u9264\u5b50\u7684\u751f\u547d\u9031\u671f\u53ca\u5176\u5728 WordPress \u4e2d\u7684\u89d2\u8272\uff0c\u5c0d\u65bc\u6210\u529f\u64f4\u5145 API \u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p><figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.22.40-A-clear-educational-illustration-showing-the-role-of-the-rest_api_init-action-hook-in-WordPress-focusing-on-adding-custom-routes-and-endpoints-to-.webp\" alt=\"\" class=\"wp-image-4320\" srcset=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.22.40-A-clear-educational-illustration-showing-the-role-of-the-rest_api_init-action-hook-in-WordPress-focusing-on-adding-custom-routes-and-endpoints-to-.webp 1024w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.22.40-A-clear-educational-illustration-showing-the-role-of-the-rest_api_init-action-hook-in-WordPress-focusing-on-adding-custom-routes-and-endpoints-to--600x600.webp 600w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.22.40-A-clear-educational-illustration-showing-the-role-of-the-rest_api_init-action-hook-in-WordPress-focusing-on-adding-custom-routes-and-endpoints-to--100x100.webp 100w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.22.40-A-clear-educational-illustration-showing-the-role-of-the-rest_api_init-action-hook-in-WordPress-focusing-on-adding-custom-routes-and-endpoints-to--300x300.webp 300w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.22.40-A-clear-educational-illustration-showing-the-role-of-the-rest_api_init-action-hook-in-WordPress-focusing-on-adding-custom-routes-and-endpoints-to--150x150.webp 150w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.22.40-A-clear-educational-illustration-showing-the-role-of-the-rest_api_init-action-hook-in-WordPress-focusing-on-adding-custom-routes-and-endpoints-to--768x768.webp 768w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.22.40-A-clear-educational-illustration-showing-the-role-of-the-rest_api_init-action-hook-in-WordPress-focusing-on-adding-custom-routes-and-endpoints-to--12x12.webp 12w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.22.40-A-clear-educational-illustration-showing-the-role-of-the-rest_api_init-action-hook-in-WordPress-focusing-on-adding-custom-routes-and-endpoints-to--96x96.webp 96w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><h3 class=\"wp-block-heading\">rest_api_init \u7121\u6cd5\u904b\u4f5c\u7684\u5e38\u898b\u539f\u56e0<\/h3><p>\u8b93\u6211\u5011\u4f86\u5206\u6790\u4e00\u4e0b\u6700\u5e38\u898b\u7684\u539f\u56e0 <code>rest_api_init<\/code> \u5728\u60a8\u7684\u81ea\u8a02 WordPress \u5916\u639b\u4e2d\u53ef\u80fd\u7121\u6cd5\u904b\u4f5c\uff0c\u4e26\u91dd\u5c0d\u6bcf\u500b\u554f\u984c\u63d0\u4f9b\u5be6\u7528\u7684\u89e3\u6c7a\u65b9\u6848\u3002<\/p><h4 class=\"wp-block-heading\">1.\u639b\u9264\u4f4d\u7f6e\u548c\u6642\u9593\u554f\u984c<\/h4><p>\u6700\u5e38\u898b\u7684\u539f\u56e0\u662f <code>rest_api_init<\/code> \u4e0d\u5de5\u4f5c\u7684\u539f\u56e0\u662f\u639b\u9264\u4f4d\u7f6e\u4e0d\u6b63\u78ba\u3002\u7684 <code>rest_api_init<\/code> \u52d5\u4f5c\u9264\u9700\u8981\u5728 WordPress \u521d\u59cb\u5316\u6240\u6709\u5143\u4ef6\u4e4b\u5f8c\u547c\u53eb\u3002\u4e00\u822c\u800c\u8a00\uff0c\u60a8\u61c9\u8a72\u5728\u4e3b\u5916\u639b\u6a94\u6848\u4e2d\u52a0\u5165\u9019\u500b\u52d5\u4f5c\uff0c\u6216\u662f\u5728\u55ae\u7368\u7684\u521d\u59cb\u5316\u51fd\u5f0f\u4e2d\u52a0\u5165\uff0c\u78ba\u4fdd\u5b83\u662f\u5728 WordPress \u6838\u5fc3\u548c\u4efb\u4f55\u76f8\u4f9d\u5916\u639b\u5b8c\u5168\u8f09\u5165\u5f8c\u624d\u88ab\u547c\u53eb\u3002<\/p><figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.27.55-A-clear-and-modern-illustration-explaining-the-correct-usage-of-the-rest_api_init-action-hook-in-WordPress-plugin-development-focused-on-correct-ho.webp\" alt=\"\" class=\"wp-image-4323\" srcset=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.27.55-A-clear-and-modern-illustration-explaining-the-correct-usage-of-the-rest_api_init-action-hook-in-WordPress-plugin-development-focused-on-correct-ho.webp 1024w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.27.55-A-clear-and-modern-illustration-explaining-the-correct-usage-of-the-rest_api_init-action-hook-in-WordPress-plugin-development-focused-on-correct-ho-600x600.webp 600w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.27.55-A-clear-and-modern-illustration-explaining-the-correct-usage-of-the-rest_api_init-action-hook-in-WordPress-plugin-development-focused-on-correct-ho-100x100.webp 100w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.27.55-A-clear-and-modern-illustration-explaining-the-correct-usage-of-the-rest_api_init-action-hook-in-WordPress-plugin-development-focused-on-correct-ho-300x300.webp 300w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.27.55-A-clear-and-modern-illustration-explaining-the-correct-usage-of-the-rest_api_init-action-hook-in-WordPress-plugin-development-focused-on-correct-ho-150x150.webp 150w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.27.55-A-clear-and-modern-illustration-explaining-the-correct-usage-of-the-rest_api_init-action-hook-in-WordPress-plugin-development-focused-on-correct-ho-768x768.webp 768w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.27.55-A-clear-and-modern-illustration-explaining-the-correct-usage-of-the-rest_api_init-action-hook-in-WordPress-plugin-development-focused-on-correct-ho-12x12.webp 12w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.27.55-A-clear-and-modern-illustration-explaining-the-correct-usage-of-the-rest_api_init-action-hook-in-WordPress-plugin-development-focused-on-correct-ho-96x96.webp 96w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><p><strong>\u89e3\u6c7a\u65b9\u6848\uff1a<\/strong> \u8acb\u78ba\u5b9a\u60a8\u4f7f\u7528\u4ee5\u4e0b\u65b9\u5f0f\u8a3b\u518a\u81ea\u8a02\u8def\u7531 <code>rest_api_init<\/code> \u5728\u6b63\u78ba\u7684\u4e0a\u4e0b\u6587\u4e2d\uff1a<\/p><pre class=\"wp-block-code\"><code>add_action('rest_api_init', 'register_custom_api_routes')\uff1b\n\nfunction register_custom_api_routes() {\n    register_rest_route('myplugin\/v1', '\/content\/', array(\n        'methods' =&gt; 'GET'\u3001\n        'callback' =&gt; 'get_custom_content'\u3001\n        'permission_callback' =&gt; '__return_true', \/\/ \u8a2d\u5b9a\u9069\u7576\u7684\u6b0a\u9650\n    ));\n}<\/code><\/pre><p><strong>\u8aaa\u660e\uff1a<\/strong><\/p><ol start=\"1\" class=\"wp-block-list\"><li><code>add_action('rest_api_init', 'register_custom_api_routes')\uff1b<\/code> - \u6b64\u884c\u639b\u63a5\u5230 <code>rest_api_init<\/code> \u4ee5\u5728 REST API \u521d\u59cb\u5316\u5f8c\u8a3b\u518a\u6211\u5011\u7684\u81ea\u8a02\u8def\u7531\u3002<\/li>\n\n<li><code>register_rest_route('myplugin\/v1', '\/content\/', ...)<\/code> - \u6b64\u529f\u80fd\u6703\u8a3b\u518a\u65b0\u7684 REST API \u8def\u7531\u3002\u547d\u540d\u7a7a\u9593 (<code>'myplugin\/v1'<\/code>) \u6709\u52a9\u65bc\u552f\u4e00\u8b58\u5225\u8def\u7531\uff0c\u907f\u514d\u8207\u5176\u4ed6\u5916\u639b\u7522\u751f\u885d\u7a81\u3002<\/li>\n\n<li><code>'permission_callback' =&gt; '__return_true'<\/code> - \u9019\u500b\u56de\u547c\u6703\u6388\u4e88\u4efb\u4f55\u5b58\u53d6\u7aef\u9ede\u7684\u4eba\u6b0a\u9650\u3002\u5c0d\u65bc\u5be6\u969b\u61c9\u7528\u7a0b\u5f0f\uff0c\u8acb\u4ee5\u9069\u7576\u7684\u4f7f\u7528\u8005\u80fd\u529b\u6aa2\u67e5\u4f86\u53d6\u4ee3\uff0c\u4ee5\u78ba\u4fdd\u5b89\u5168\u6027\u3002<\/li><\/ol><figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/7b189a88-66a0-426a-a492-ef33c9639f77.webp\" alt=\"\" class=\"wp-image-4324\" srcset=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/7b189a88-66a0-426a-a492-ef33c9639f77.webp 1024w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/7b189a88-66a0-426a-a492-ef33c9639f77-600x600.webp 600w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/7b189a88-66a0-426a-a492-ef33c9639f77-100x100.webp 100w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/7b189a88-66a0-426a-a492-ef33c9639f77-300x300.webp 300w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/7b189a88-66a0-426a-a492-ef33c9639f77-150x150.webp 150w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/7b189a88-66a0-426a-a492-ef33c9639f77-768x768.webp 768w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/7b189a88-66a0-426a-a492-ef33c9639f77-12x12.webp 12w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/7b189a88-66a0-426a-a492-ef33c9639f77-96x96.webp 96w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><p>\u5c07\u6b64\u7a0b\u5f0f\u78bc\u653e\u7f6e\u5728\u4e3b\u5916\u639b\u7a0b\u5f0f\u6a94\u6848\u4e2d\uff0c\u6216\u662f\u5728\u8f09\u5165\u6240\u6709\u5916\u639b\u7a0b\u5f0f\u5f8c\u57f7\u884c\u7684\u51fd\u5f0f\u4e2d\uff0c\u4f8b\u5982 <code>plugins_loaded<\/code>.\u639b\u52fe\u4f4d\u7f6e\u4e0d\u6b63\u78ba\u53ef\u80fd\u5c0e\u81f4\u8def\u7531\u7121\u6cd5\u6b63\u78ba\u767b\u9304\u3002<\/p><h4 class=\"wp-block-heading\">2.\u5916\u639b\u7a0b\u5f0f\u6216\u4e3b\u984c\u885d\u7a81<\/h4><p>\u53e6\u4e00\u500b\u5e38\u898b\u539f\u56e0\u662f <code>rest_api_init<\/code> \u7121\u6cd5\u6b63\u5e38\u904b\u4f5c\u53ef\u80fd\u662f\u56e0\u70ba\u8207\u5176\u4ed6\u4e5f\u8a3b\u518a\u8def\u7531\u7684\u5916\u639b\u7a0b\u5f0f\u6216\u4e3b\u984c\u7522\u751f\u885d\u7a81\u3002\u5982\u679c\u5169\u500b\u5916\u639b\u7a0b\u5f0f\u5617\u8a66\u8a3b\u518a\u76f8\u540c\u7684\u7aef\u9ede\uff0c\u6216\u4e3b\u984c\u6709\u985e\u4f3c\u7684\u8def\u7531\uff0c\u5c31\u53ef\u80fd\u6703\u7522\u751f\u885d\u7a81\uff0c\u5c0e\u81f4\u60a8\u7684\u81ea\u8a02\u7aef\u9ede\u7121\u6cd5\u6b63\u5e38\u904b\u4f5c\u3002<\/p><figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.37.23-An-educational-illustration-explaining-the-importance-of-unique-namespace-prefixes-in-WordPress-REST-API-routes-to-avoid-conflicts.-Show-a-developers.webp\" alt=\"\" class=\"wp-image-4326\" srcset=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.37.23-An-educational-illustration-explaining-the-importance-of-unique-namespace-prefixes-in-WordPress-REST-API-routes-to-avoid-conflicts.-Show-a-developers.webp 1024w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.37.23-An-educational-illustration-explaining-the-importance-of-unique-namespace-prefixes-in-WordPress-REST-API-routes-to-avoid-conflicts.-Show-a-developers-600x600.webp 600w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.37.23-An-educational-illustration-explaining-the-importance-of-unique-namespace-prefixes-in-WordPress-REST-API-routes-to-avoid-conflicts.-Show-a-developers-100x100.webp 100w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.37.23-An-educational-illustration-explaining-the-importance-of-unique-namespace-prefixes-in-WordPress-REST-API-routes-to-avoid-conflicts.-Show-a-developers-300x300.webp 300w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.37.23-An-educational-illustration-explaining-the-importance-of-unique-namespace-prefixes-in-WordPress-REST-API-routes-to-avoid-conflicts.-Show-a-developers-150x150.webp 150w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.37.23-An-educational-illustration-explaining-the-importance-of-unique-namespace-prefixes-in-WordPress-REST-API-routes-to-avoid-conflicts.-Show-a-developers-768x768.webp 768w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.37.23-An-educational-illustration-explaining-the-importance-of-unique-namespace-prefixes-in-WordPress-REST-API-routes-to-avoid-conflicts.-Show-a-developers-12x12.webp 12w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.37.23-An-educational-illustration-explaining-the-importance-of-unique-namespace-prefixes-in-WordPress-REST-API-routes-to-avoid-conflicts.-Show-a-developers-96x96.webp 96w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><p><strong>\u89e3\u6c7a\u65b9\u6848\uff1a<\/strong> \u4f7f\u7528\u552f\u4e00\u7684\u547d\u540d\u7a7a\u9593\u524d\u7db4\u4ee5\u907f\u514d\u885d\u7a81\u3002REST API \u8def\u7531\u4e2d\u7684\u547d\u540d\u7a7a\u9593\u65e8\u5728\u9632\u6b62\u6b64\u985e\u885d\u7a81\u3002\u4f8b\u5982<\/p><pre class=\"wp-block-code\"><code>register_rest_route('myplugin\/v1', '\/custom-content\/', array(\n    'methods' =&gt; 'GET'\u3001\n    'callback' =&gt; 'get_custom_content'\u3001\n    'permission_callback' =&gt; '__return_true'\u3001\n));<\/code><\/pre><p><strong>\u8aaa\u660e\uff1a<\/strong><\/p><ul class=\"wp-block-list\"><li>\u7684 <code>'myplugin\/v1'<\/code> \u547d\u540d\u7a7a\u9593\u6709\u52a9\u65bc\u552f\u4e00\u8b58\u5225\u8def\u7531\uff0c\u9632\u6b62\u547d\u540d\u885d\u7a81\u3002<\/li>\n\n<li>\u5728\u505c\u7528\u885d\u7a81\u5916\u639b\u7a0b\u5f0f\u7684\u60c5\u6cc1\u4e0b\u9032\u884c\u6e2c\u8a66\uff0c\u6709\u52a9\u65bc\u9694\u96e2\u7279\u5b9a\u5916\u639b\u7a0b\u5f0f\u662f\u5426\u9020\u6210\u554f\u984c\u3002<\/li><\/ul><h4 class=\"wp-block-heading\">3.\u8a31\u53ef\u8b49\u7d50\u69cb\u8a2d\u5b9a\u4e0d\u6b63\u78ba<\/h4><p>WordPress REST API \u4f9d\u8cf4\u6c38\u4e45\u9023\u7d50\u7d50\u69cb\u624d\u80fd\u6b63\u5e38\u904b\u4f5c\u3002\u5982\u679c\u60a8\u7684\u7db2\u7ad9\u4f7f\u7528\u666e\u901a\u7684\u6c38\u4e45\u9023\u7d50\uff0cREST API \u8def\u7531\u53ef\u80fd\u7121\u6cd5\u4f7f\u7528\u6216\u7121\u6cd5\u5982\u9810\u671f\u822c\u904b\u4f5c\u3002<\/p><p><strong>\u89e3\u6c7a\u65b9\u6848\uff1a<\/strong> \u66f4\u65b0\u60a8\u7684\u6a19\u984c\u9023\u7d50\u7d50\u69cb\uff0c\u8acb\u5c0e\u822a\u81f3 <strong>\u8a2d\u5b9a &gt; \u6c38\u4e45\u9023\u7d50<\/strong> \u4e2d\u7684 WordPress \u7ba1\u7406\u9762\u677f\u3002\u9078\u64c7\u300cPlain\u300d\u4ee5\u5916\u7684\u4efb\u4f55\u7d50\u69cb\uff0c\u7136\u5f8c\u5132\u5b58\u8b8a\u66f4\u3002\u9019\u6703\u5237\u65b0\u91cd\u5beb\u898f\u5247\uff0c\u4e26\u78ba\u4fdd\u60a8\u7684 REST API \u8def\u7531\u53ef\u88ab\u5b58\u53d6\u3002<\/p><figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.37.34-An-informative-illustration-showing-the-importance-of-setting-the-correct-permalink-structure-in-WordPress-for-REST-API-functionality.-Show-a-WordPres.webp\" alt=\"\" class=\"wp-image-4325\" srcset=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.37.34-An-informative-illustration-showing-the-importance-of-setting-the-correct-permalink-structure-in-WordPress-for-REST-API-functionality.-Show-a-WordPres.webp 1024w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.37.34-An-informative-illustration-showing-the-importance-of-setting-the-correct-permalink-structure-in-WordPress-for-REST-API-functionality.-Show-a-WordPres-600x600.webp 600w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.37.34-An-informative-illustration-showing-the-importance-of-setting-the-correct-permalink-structure-in-WordPress-for-REST-API-functionality.-Show-a-WordPres-100x100.webp 100w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.37.34-An-informative-illustration-showing-the-importance-of-setting-the-correct-permalink-structure-in-WordPress-for-REST-API-functionality.-Show-a-WordPres-300x300.webp 300w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.37.34-An-informative-illustration-showing-the-importance-of-setting-the-correct-permalink-structure-in-WordPress-for-REST-API-functionality.-Show-a-WordPres-150x150.webp 150w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.37.34-An-informative-illustration-showing-the-importance-of-setting-the-correct-permalink-structure-in-WordPress-for-REST-API-functionality.-Show-a-WordPres-768x768.webp 768w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.37.34-An-informative-illustration-showing-the-importance-of-setting-the-correct-permalink-structure-in-WordPress-for-REST-API-functionality.-Show-a-WordPres-12x12.webp 12w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.37.34-An-informative-illustration-showing-the-importance-of-setting-the-correct-permalink-structure-in-WordPress-for-REST-API-functionality.-Show-a-WordPres-96x96.webp 96w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><h4 class=\"wp-block-heading\">4.\u5feb\u53d6\u554f\u984c<\/h4><p>\u5feb\u53d6\u5916\u639b\u7a0b\u5f0f\u6216\u4f3a\u670d\u5668\u5c64\u7d1a\u7684\u5feb\u53d6\u6703\u5e72\u64fe REST API \u7aef\u9ede\u7684\u6b63\u5e38\u904b\u4f5c\u3002\u555f\u7528\u5feb\u53d6\u529f\u80fd\u6642\uff0c\u8def\u7531\u7684\u8b8a\u66f4\u53ef\u80fd\u7121\u6cd5\u7acb\u5373\u53cd\u6620\uff0c\u5c0e\u81f4\u60a8\u89ba\u5f97 <code>rest_api_init<\/code> \u7121\u6cd5\u904b\u4f5c\u3002<\/p><p><strong>\u89e3\u6c7a\u65b9\u6848\uff1a<\/strong> \u6e05\u9664\u4efb\u4f55\u5feb\u53d6\u6a5f\u5236\uff0c\u5305\u62ec\u4f3a\u670d\u5668\u7aef\uff08\u4f8b\u5982 Varnish \u6216 Nginx \u5feb\u53d6\uff09\u548c WordPress \u5c64\u7d1a\u7684\u5feb\u53d6\u5916\u639b\u3002\u5728\u958b\u767c\u74b0\u5883\u4e2d\uff0c\u8acb\u8003\u616e\u5b8c\u5168\u505c\u7528\u5feb\u53d6\uff0c\u4ee5\u907f\u514d\u5728\u958b\u767c\u548c\u6e2c\u8a66\u671f\u9593\u767c\u751f\u554f\u984c\u3002<\/p><figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.44.55-An-educational-illustration-focusing-on-resolving-caching-issues-and-permission-callback-errors-in-WordPress-REST-API-development.-Show-a-workspace-wi.webp\" alt=\"\" class=\"wp-image-4331\" srcset=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.44.55-An-educational-illustration-focusing-on-resolving-caching-issues-and-permission-callback-errors-in-WordPress-REST-API-development.-Show-a-workspace-wi.webp 1024w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.44.55-An-educational-illustration-focusing-on-resolving-caching-issues-and-permission-callback-errors-in-WordPress-REST-API-development.-Show-a-workspace-wi-600x600.webp 600w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.44.55-An-educational-illustration-focusing-on-resolving-caching-issues-and-permission-callback-errors-in-WordPress-REST-API-development.-Show-a-workspace-wi-100x100.webp 100w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.44.55-An-educational-illustration-focusing-on-resolving-caching-issues-and-permission-callback-errors-in-WordPress-REST-API-development.-Show-a-workspace-wi-300x300.webp 300w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.44.55-An-educational-illustration-focusing-on-resolving-caching-issues-and-permission-callback-errors-in-WordPress-REST-API-development.-Show-a-workspace-wi-150x150.webp 150w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.44.55-An-educational-illustration-focusing-on-resolving-caching-issues-and-permission-callback-errors-in-WordPress-REST-API-development.-Show-a-workspace-wi-768x768.webp 768w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.44.55-An-educational-illustration-focusing-on-resolving-caching-issues-and-permission-callback-errors-in-WordPress-REST-API-development.-Show-a-workspace-wi-12x12.webp 12w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.44.55-An-educational-illustration-focusing-on-resolving-caching-issues-and-permission-callback-errors-in-WordPress-REST-API-development.-Show-a-workspace-wi-96x96.webp 96w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><h4 class=\"wp-block-heading\">5.\u4e0d\u6b63\u78ba\u7684\u6b0a\u9650\u56de\u547c<\/h4><p>\u7684 <code>permission_callback<\/code> \u53c3\u6578\u7528\u65bc\u5224\u65b7\u4f7f\u7528\u8005\u662f\u5426\u53ef\u4ee5\u5b58\u53d6 REST API \u7aef\u9ede\u3002\u5982\u679c\u6b64\u56de\u8abf\u51fd\u5f0f\u56de\u50b3 false \u6216\u9047\u5230\u932f\u8aa4\uff0c\u5247\u7121\u6cd5\u5b58\u53d6\u7aef\u9ede\uff0c\u7d66\u4eba\u7684\u5370\u8c61\u662f <code>rest_api_init<\/code> \u6c92\u6709\u7528\u3002<\/p><p><strong>\u89e3\u6c7a\u65b9\u6848\uff1a<\/strong> \u9a57\u8b49 <code>permission_callback<\/code> \u51fd\u6578\u5df2\u6b63\u78ba\u57f7\u884c\uff0c\u4e26\u8fd4\u56de <code>\u771f<\/code> \u5982\u679c\u5141\u8a31\u5b58\u53d6\u3002\u4f8b\u5982<\/p><pre class=\"wp-block-code\"><code>function get_custom_content_permission() {\n    return current_user_can('read'); \/\/ \u6839\u64da\u9700\u8981\u8abf\u6574\u80fd\u529b\n}\n\nadd_action('rest_api_init', 'register_custom_api_routes')\uff1b\n\nfunction register_custom_api_routes() {\n    register_rest_route('myplugin\/v1', '\/custom-content\/', array(\n        'methods' =&gt; 'GET'\u3001\n        'callback' =&gt; 'get_custom_content'\u3001\n        'permission_callback' =&gt; 'get_custom_content_permission'\u3001\n    ));\n}<\/code><\/pre><p><strong>\u8aaa\u660e\uff1a<\/strong><\/p><ul class=\"wp-block-list\"><li><code>current_user_can('read')<\/code> \u6aa2\u67e5\u76ee\u524d\u4f7f\u7528\u8005\u662f\u5426\u6709\u9069\u7576\u7684\u6b0a\u9650\u3002\u8acb\u91dd\u5c0d\u60a8\u7684\u7aef\u9ede\u8abf\u6574\u5fc5\u8981\u7684\u80fd\u529b\u3002<\/li>\n\n<li>\u5982\u679c\u6c92\u6709\u6240\u9700\u6b0a\u9650\u7684\u4f7f\u7528\u8005\u5617\u8a66\u5b58\u53d6\u7aef\u9ede\uff0c\u5c31\u6703\u51fa\u73fe\u932f\u8aa4\uff0c\u9019\u6709\u52a9\u65bc\u4fdd\u8b77\u60a8\u7684 API\u3002<\/li><\/ul><h4 class=\"wp-block-heading\">6.\u8abf\u8a66\u5de5\u5177\u8207\u6280\u8853<\/h4><p>\u5982\u679c\u6c92\u6709\u9069\u7576\u7684\u5de5\u5177\uff0c\u8abf\u8a66 REST API \u554f\u984c\u53ef\u80fd\u6703\u5f88\u5177\u6311\u6230\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u8abf\u8a66\u65b9\u6cd5 <code>rest_api_init<\/code> \u53ef\u80fd\u7121\u6cd5\u904b\u4f5c\uff1a<\/p><ul class=\"wp-block-list\"><li><strong>WordPress \u9664\u932f\u6a21\u5f0f<\/strong>:\u555f\u7528 WordPress \u932f\u8aa4\u6a21\u5f0f\uff0c\u52a0\u5165 <code>define('WP_DEBUG', true)\uff1b<\/code> \u5728\u60a8\u7684 <code>wp-config.php<\/code> \u6a94\u6848\u3002\u9019\u53ef\u5354\u52a9\u60a8\u627e\u51fa\u5916\u639b\u7a0b\u5f0f\u78bc\u4e2d\u53ef\u80fd\u6703\u963b\u6b62 <code>rest_api_init<\/code> \u7121\u6cd5\u6b63\u78ba\u767c\u5c04\u3002<\/li>\n\n<li><strong>\u4f7f\u7528 Postman \u9032\u884c\u6e2c\u8a66<\/strong>:Postman \u662f\u6e2c\u8a66 REST API \u7aef\u9ede\u7684\u7d55\u4f73\u5de5\u5177\u3002\u8981\u6e2c\u8a66\u60a8\u7684\u81ea\u8a02\u8def\u7531\uff0c\u8acb\u50b3\u9001 GET \u8acb\u6c42\u5230\u60a8\u7684\u7aef\u9ede URL (\u4f8b\u5982\u3001 <code>http:\/\/yourdomain.com\/wp-json\/myplugin\/v1\/custom-content<\/code>).\u9019\u53ef\u5354\u52a9\u60a8\u67e5\u770b\u7aef\u9ede\u662f\u5426\u53ef\u5b58\u53d6\uff0c\u4ee5\u53ca\u662f\u5426\u50b3\u56de\u9810\u671f\u7684\u8cc7\u6599\u3002<ul class=\"wp-block-list\"><li><strong>\u9010\u6b65\u6e2c\u8a66\u90f5\u5dee<\/strong>:<ol start=\"1\" class=\"wp-block-list\"><li>\u958b\u555f Postman \u4e26\u5efa\u7acb\u65b0\u7684 GET \u8acb\u6c42\u3002<\/li>\n\n<li>\u8f38\u5165\u60a8\u7684 API \u7aef\u9ede URL (\u4f8b\u5982\u3001 <code>http:\/\/yourdomain.com\/wp-json\/myplugin\/v1\/custom-content<\/code>).<\/li>\n\n<li>\u6309\u4e00\u4e0b\u300c\u50b3\u9001\u300d\u4ee5\u555f\u52d5\u8acb\u6c42\u3002<\/li>\n\n<li>\u89c0\u5bdf\u56de\u61c9\u72c0\u614b\u4ee3\u78bc\u548c\u8cc7\u6599\u3002200 OK \u72c0\u614b\u8868\u793a\u6210\u529f\uff0c\u800c 404 \u6216 403 \u5247\u8868\u793a\u8def\u7531\u672a\u627e\u5230\u6216\u6b0a\u9650\u932f\u8aa4\u7b49\u554f\u984c\u3002<\/li>\n\n<li>\u5982\u679c\u60a8\u7684\u7aef\u9ede\u9700\u8981\u9a57\u8b49\uff0c\u8acb\u5305\u542b\u5fc5\u8981\u7684\u6a19\u982d\uff0c\u4f8b\u5982\u6388\u6b0a\u6a19\u8a18\uff0c\u4ee5\u5b58\u53d6\u53d7\u9650\u5236\u7684\u8def\u7531\u3002<\/li><\/ol><\/li><\/ul><\/li>\n\n<li><strong>WordPress REST API \u63a7\u5236\u53f0\u5916\u639b\u7a0b\u5f0f<\/strong>:REST API Console \u5916\u639b\u7a0b\u5f0f\u53ef\u7528\u65bc\u76f4\u63a5\u5f9e\u7ba1\u7406\u9762\u677f\u8207 WordPress REST API \u9032\u884c\u4e92\u52d5\u3002\u9019\u6709\u52a9\u65bc\u5373\u6642\u9664\u932f\u548c\u6aa2\u67e5\u60a8\u7684\u81ea\u8a02\u8def\u7531\u662f\u5426\u6b63\u78ba\u8a3b\u518a\u3002<\/li>\n\n<li><strong>\u7528\u65bc\u547d\u4ee4\u5217\u6e2c\u8a66\u7684 cURL<\/strong>:\u60a8\u4e5f\u53ef\u4ee5\u5f9e\u547d\u4ee4\u5217\u4f7f\u7528 cURL \u4f86\u6e2c\u8a66\u60a8\u7684\u7aef\u9ede\uff1a<code>curl -X GET \"http:\/\/yourdomain.com\/wp-json\/myplugin\/v1\/custom-content\"<\/code><strong>\u8aaa\u660e\uff1a<\/strong> \u6b64\u6307\u4ee4\u6703\u5411\u60a8\u7684\u7aef\u9ede\u50b3\u9001 GET \u8acb\u6c42\u3002\u5b83\u53ef\u8b93\u60a8\u9a57\u8b49\u8def\u7531\u662f\u5426\u53ef\u5b58\u53d6\uff0c\u4e26\u6392\u9664\u4efb\u4f55\u7db2\u8def\u5c64\u7d1a\u7684\u554f\u984c\u3002\u8acb\u6ce8\u610f\u56de\u61c9\u6a19\u982d\u548c\u72c0\u614b\u4ee3\u78bc\u3002<\/li><\/ul><figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.40.11-An-instructional-illustration-showing-various-debugging-tools-and-techniques-for-troubleshooting-WordPress-REST-API-issues-specifically-focusing-on-t.webp\" alt=\"\" class=\"wp-image-4327\" srcset=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.40.11-An-instructional-illustration-showing-various-debugging-tools-and-techniques-for-troubleshooting-WordPress-REST-API-issues-specifically-focusing-on-t.webp 1024w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.40.11-An-instructional-illustration-showing-various-debugging-tools-and-techniques-for-troubleshooting-WordPress-REST-API-issues-specifically-focusing-on-t-600x600.webp 600w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.40.11-An-instructional-illustration-showing-various-debugging-tools-and-techniques-for-troubleshooting-WordPress-REST-API-issues-specifically-focusing-on-t-100x100.webp 100w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.40.11-An-instructional-illustration-showing-various-debugging-tools-and-techniques-for-troubleshooting-WordPress-REST-API-issues-specifically-focusing-on-t-300x300.webp 300w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.40.11-An-instructional-illustration-showing-various-debugging-tools-and-techniques-for-troubleshooting-WordPress-REST-API-issues-specifically-focusing-on-t-150x150.webp 150w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.40.11-An-instructional-illustration-showing-various-debugging-tools-and-techniques-for-troubleshooting-WordPress-REST-API-issues-specifically-focusing-on-t-768x768.webp 768w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.40.11-An-instructional-illustration-showing-various-debugging-tools-and-techniques-for-troubleshooting-WordPress-REST-API-issues-specifically-focusing-on-t-12x12.webp 12w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.40.11-An-instructional-illustration-showing-various-debugging-tools-and-techniques-for-troubleshooting-WordPress-REST-API-issues-specifically-focusing-on-t-96x96.webp 96w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><h3 class=\"wp-block-heading\">WPML \u548c Polylang \u7684\u8a73\u7d30\u5916\u639b\u7a0b\u5f0f\u6574\u5408\u6b65\u9a5f<\/h3><p>\u70ba\u4e86\u8b93\u60a8\u7684\u81ea\u8a02 REST API \u591a\u8a9e\u8a00\u5316\uff0c\u60a8\u53ef\u80fd\u6703\u4f7f\u7528 WPML \u6216 Polylang \u7b49\u5916\u639b\u7a0b\u5f0f\u3002\u96d6\u7136\u9019\u4e9b\u5916\u639b\u7a0b\u5f0f\u63d0\u4f9b\u4e86\u5168\u9762\u7684\u7ffb\u8b6f\u5de5\u5177\uff0c\u4f46\u662f\u5c07\u5b83\u5011\u8207\u81ea\u8a02 REST API \u8def\u7531\u6b63\u78ba\u6574\u5408\u9700\u8981\u7279\u5b9a\u7684\u6b65\u9a5f\u3002<\/p><h4 class=\"wp-block-heading\">WPML \u6574\u5408<\/h4><ol start=\"1\" class=\"wp-block-list\"><li><strong>\u5b89\u88dd WPML \u5916\u639b\u7a0b\u5f0f<\/strong>:\u5f9e WordPress \u5957\u4ef6\u5eab\u6216\u60a8\u5728 WPML \u7db2\u7ad9\u7684\u5e33\u6236\u5b89\u88dd\u4e26\u555f\u52d5 WPML \u5916\u639b\u7a0b\u5f0f\u3002<\/li>\n\n<li><strong>\u5207\u63db\u8a9e\u8a00\u4e0a\u4e0b\u6587<\/strong>:\u4f7f\u7528 <code>\u5168\u7403 $sitepress\uff1b<\/code> \u548c <code>$sitepress-&gt;switch_lang($language)\uff1b<\/code> \u5728\u60a8\u7684\u56de\u547c\u51fd\u6578\u4e2d\uff0c\u5728\u67e5\u8a62\u5167\u5bb9\u524d\u8a2d\u5b9a\u8a9e\u8a00\u4e0a\u4e0b\u6587\u3002<\/li>\n\n<li><strong>\u8a3b\u518a REST API \u8def\u7531<\/strong>:\u78ba\u4fdd\u60a8\u7684\u8def\u7531\u6b63\u78ba\u8655\u7406 <code>\u9577<\/code> \u53c3\u6578\uff0c\u5728\u50b3\u56de\u5167\u5bb9\u4e4b\u524d\u52d5\u614b\u5207\u63db\u8a9e\u8a00\uff1a<code>function get_content_by_language($language) { global $sitepress; $sitepress-&gt;switch_lang($language); $args = array( 'post_type' =&gt; 'post', 'posts_per_page' =&gt; 5 ); $query = new WP_Query($args); return $query-&gt;posts; }<\/code><strong>\u8aaa\u660e\uff1a<\/strong> \u6b64\u529f\u80fd\u5728\u67e5\u8a62\u6587\u7ae0\u4e4b\u524d\uff0c\u4f7f\u7528 WPML \u7684 API \u5207\u63db\u8a9e\u8a00\u4e0a\u4e0b\u6587\u3002\u9019\u53ef\u78ba\u4fdd\u4ee5\u6240\u9700\u8a9e\u8a00\u53d6\u5f97\u5167\u5bb9\u3002<\/li><\/ol><figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.41.08-A-detailed-illustration-showing-steps-to-integrate-WPML-with-custom-WordPress-REST-API-routes-for-multilingual-support.-Depict-a-developer-workspace-w.webp\" alt=\"\" class=\"wp-image-4328\" srcset=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.41.08-A-detailed-illustration-showing-steps-to-integrate-WPML-with-custom-WordPress-REST-API-routes-for-multilingual-support.-Depict-a-developer-workspace-w.webp 1024w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.41.08-A-detailed-illustration-showing-steps-to-integrate-WPML-with-custom-WordPress-REST-API-routes-for-multilingual-support.-Depict-a-developer-workspace-w-600x600.webp 600w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.41.08-A-detailed-illustration-showing-steps-to-integrate-WPML-with-custom-WordPress-REST-API-routes-for-multilingual-support.-Depict-a-developer-workspace-w-100x100.webp 100w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.41.08-A-detailed-illustration-showing-steps-to-integrate-WPML-with-custom-WordPress-REST-API-routes-for-multilingual-support.-Depict-a-developer-workspace-w-300x300.webp 300w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.41.08-A-detailed-illustration-showing-steps-to-integrate-WPML-with-custom-WordPress-REST-API-routes-for-multilingual-support.-Depict-a-developer-workspace-w-150x150.webp 150w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.41.08-A-detailed-illustration-showing-steps-to-integrate-WPML-with-custom-WordPress-REST-API-routes-for-multilingual-support.-Depict-a-developer-workspace-w-768x768.webp 768w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.41.08-A-detailed-illustration-showing-steps-to-integrate-WPML-with-custom-WordPress-REST-API-routes-for-multilingual-support.-Depict-a-developer-workspace-w-12x12.webp 12w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.41.08-A-detailed-illustration-showing-steps-to-integrate-WPML-with-custom-WordPress-REST-API-routes-for-multilingual-support.-Depict-a-developer-workspace-w-96x96.webp 96w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><h4 class=\"wp-block-heading\">Polylang \u6574\u5408<\/h4><ol start=\"1\" class=\"wp-block-list\"><li><strong>\u5b89\u88dd Polylang<\/strong>:\u5b89\u88dd\u4e26\u555f\u52d5 Polylang \u5916\u639b\u7a0b\u5f0f\u3002<\/li>\n\n<li><strong>\u4f7f\u7528 pll_set_language() \u8a2d\u5b9a\u8a9e\u8a00<\/strong>:\u4f7f\u7528 <code>pll_set_language($language)\uff1b<\/code> \u5728\u60a8\u7684 API \u56de\u8abf\u51fd\u5f0f\u4e2d\u8a2d\u5b9a\u8a9e\u8a00\u4e0a\u4e0b\u6587\u3002<\/li>\n\n<li><strong>\u4fee\u6539\u67e5\u8a62\u4ee5\u53cd\u6620\u8a9e\u8a00\u5167\u5bb9<\/strong>:\u78ba\u4fdd\u5728\u67e5\u8a62\u5167\u5bb9\u6642\uff0c\u8a9e\u8a00\u8a2d\u5b9a\u70ba\u6240\u9700\u7684\u503c\uff1a<code>function get_content_by_language($language) { pll_set_language($language); $args = array( 'post_type' =&gt; 'post', 'posts_per_page' =&gt; 5 ); $query = new WP_Query($args); return $query-&gt;posts; }<\/code><strong>\u8aaa\u660e\uff1a<\/strong> \u7684 <code>pll_set_language($language)<\/code> \u51fd\u5f0f\u70ba\u5167\u5bb9\u67e5\u8a62\u8a2d\u5b9a\u6240\u9700\u8a9e\u8a00\uff0c\u78ba\u4fdd\u50b3\u56de\u6b63\u78ba\u7248\u672c\u7684\u5167\u5bb9\u3002<\/li><\/ol><h3 class=\"wp-block-heading\">\u771f\u5be6\u4e16\u754c\u7bc4\u4f8b\uff1a\u5efa\u7acb\u591a\u8a9e\u8a00\u90e8\u843d\u683c API<\/h3><p>\u8b93\u6211\u5011\u5efa\u7acb\u4e00\u500b\u7c21\u55ae\u7684\u591a\u8a9e\u8a00\u7db2\u8a8c API\uff0c\u4f7f\u7528 <code>rest_api_init<\/code>.\u6b64 API \u5c07\u652f\u63f4\u4f7f\u7528 WPML \u67e5\u8a62\u4e0d\u540c\u8a9e\u8a00\u7684\u6587\u7ae0\u3002<\/p><figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.42.36-An-informative-illustration-showing-steps-to-integrate-Polylang-with-custom-WordPress-REST-API-routes-for-multilingual-support.-Display-a-developer-wo.webp\" alt=\"\" class=\"wp-image-4329\" srcset=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.42.36-An-informative-illustration-showing-steps-to-integrate-Polylang-with-custom-WordPress-REST-API-routes-for-multilingual-support.-Display-a-developer-wo.webp 1024w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.42.36-An-informative-illustration-showing-steps-to-integrate-Polylang-with-custom-WordPress-REST-API-routes-for-multilingual-support.-Display-a-developer-wo-600x600.webp 600w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.42.36-An-informative-illustration-showing-steps-to-integrate-Polylang-with-custom-WordPress-REST-API-routes-for-multilingual-support.-Display-a-developer-wo-100x100.webp 100w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.42.36-An-informative-illustration-showing-steps-to-integrate-Polylang-with-custom-WordPress-REST-API-routes-for-multilingual-support.-Display-a-developer-wo-300x300.webp 300w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.42.36-An-informative-illustration-showing-steps-to-integrate-Polylang-with-custom-WordPress-REST-API-routes-for-multilingual-support.-Display-a-developer-wo-150x150.webp 150w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.42.36-An-informative-illustration-showing-steps-to-integrate-Polylang-with-custom-WordPress-REST-API-routes-for-multilingual-support.-Display-a-developer-wo-768x768.webp 768w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.42.36-An-informative-illustration-showing-steps-to-integrate-Polylang-with-custom-WordPress-REST-API-routes-for-multilingual-support.-Display-a-developer-wo-12x12.webp 12w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.42.36-An-informative-illustration-showing-steps-to-integrate-Polylang-with-custom-WordPress-REST-API-routes-for-multilingual-support.-Display-a-developer-wo-96x96.webp 96w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><p><strong>\u6b65\u9a5f 1\uff1a\u8a3b\u518a REST \u8def\u7531<\/strong><\/p><pre class=\"wp-block-code\"><code>add_action('rest_api_init', 'register_multilingual_blog_routes')\uff1b\n\nfunction register_multilingual_blog_routes() {\n    register_rest_route('myblog\/v1', '\/posts\/', array(\n        'methods' =&gt; 'GET'\u3001\n        'callback' =&gt; 'get_blog_posts_by_language'\u3001\n        'permission_callback' =&gt; '__return_true'\u3001\n    ));\n}<\/code><\/pre><p><strong>\u6b65\u9a5f 2\uff1a\u5b9a\u7fa9\u56de\u547c\u51fd\u5f0f<\/strong><\/p><pre class=\"wp-block-code\"><code>function get_blog_posts_by_language($request) {\n    $language = $request-&gt;get_param('lang')\uff1b\n    \n    if (!$language) {\n        return new WP_Error('no_language', 'Language parameter is required', array('status' =&gt; 400))\uff1b\n    }\n    \n    global $sitepress\uff1b\n    $sitepress-&gt;switch_lang($language)\uff1b\n    \n    $args = array(\n        'post_type' =&gt; 'post'\u3001\n        'posts_per_page' =&gt; 5\n    );\n    \n    $query = new WP_Query($args)\uff1b\n    \n    if (empty($query-&gt;posts)) {\n        return new WP_Error('no_posts', 'No posts found for the specified language', array('status' =&gt; 404))\uff1b\n    }\n    \n    return rest_ensure_response($query-&gt;posts)\uff1b\n}<\/code><\/pre><p><strong>\u8aaa\u660e\uff1a<\/strong><\/p><ul class=\"wp-block-list\"><li>\u672c\u7bc4\u4f8b\u5efa\u7acb\u4e00\u500b REST API \u7aef\u9ede <code>\/wp-json\/myblog\/v1\/posts\/<\/code> \u63a5\u53d7\u8a9e\u8a00\u53c3\u6578 (<code>\u9577<\/code>).<\/li>\n\n<li>\u7684 <code>\u4f9d\u8a9e\u8a00\u53d6\u5f97\u90e8\u843d\u683c\u6587\u7ae0<\/code> \u51fd\u6578\u4f7f\u7528 WPML \u5207\u63db\u8a9e\u8a00\u4e0a\u4e0b\u6587\uff0c\u4e26\u64da\u6b64\u67e5\u8a62\u6587\u7ae0\u3002<\/li>\n\n<li>\u5305\u542b\u9069\u7576\u7684\u932f\u8aa4\u8655\u7406\uff0c\u4ee5\u78ba\u4fdd\u5728\u627e\u4e0d\u5230\u8a9e\u8a00\u6216\u6587\u7ae0\u6642\uff0c\u7aef\u9ede\u6703\u50b3\u56de\u6709\u610f\u7fa9\u7684\u56de\u61c9\u3002<\/li><\/ul><h3 class=\"wp-block-heading\">\u7e3d\u7d50<\/h3><p>\u7684 <code>rest_api_init<\/code> hook\u662f\u64f4\u5145WordPress REST API\u7684\u5f37\u5927\u5de5\u5177\uff0c\u4f46\u5176\u6b63\u78ba\u5be6\u4f5c\u9700\u8981\u4ed4\u7d30\u8003\u616e\u6642\u9593\u3001\u885d\u7a81\u3001\u6b0a\u9650\u53ca\u9664\u932f\u6280\u8853\u3002\u900f\u904e\u78ba\u4fdd\u60a8\u7684\u9264\u5b50\u653e\u7f6e\u6b63\u78ba\u3001\u7ba1\u7406\u6f5b\u5728\u885d\u7a81\u3001\u77ad\u89e3\u6c38\u4e45\u9023\u7d50\u7684\u4f5c\u7528\uff0c\u4ee5\u53ca\u4f7f\u7528\u5f37\u5927\u7684\u9664\u932f\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u78ba\u4fdd\u60a8\u7684\u81ea\u8a02 REST API \u7aef\u9ede\u6709\u6548\u904b\u4f5c\u3002<\/p><figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.43.34-A-concluding-illustration-summarizing-best-practices-for-using-rest_api_init-in-WordPress-REST-API-development.-Show-a-professional-workspace-with-a.webp\" alt=\"\" class=\"wp-image-4330\" srcset=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.43.34-A-concluding-illustration-summarizing-best-practices-for-using-rest_api_init-in-WordPress-REST-API-development.-Show-a-professional-workspace-with-a.webp 1024w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.43.34-A-concluding-illustration-summarizing-best-practices-for-using-rest_api_init-in-WordPress-REST-API-development.-Show-a-professional-workspace-with-a-600x600.webp 600w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.43.34-A-concluding-illustration-summarizing-best-practices-for-using-rest_api_init-in-WordPress-REST-API-development.-Show-a-professional-workspace-with-a-100x100.webp 100w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.43.34-A-concluding-illustration-summarizing-best-practices-for-using-rest_api_init-in-WordPress-REST-API-development.-Show-a-professional-workspace-with-a-300x300.webp 300w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.43.34-A-concluding-illustration-summarizing-best-practices-for-using-rest_api_init-in-WordPress-REST-API-development.-Show-a-professional-workspace-with-a-150x150.webp 150w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.43.34-A-concluding-illustration-summarizing-best-practices-for-using-rest_api_init-in-WordPress-REST-API-development.-Show-a-professional-workspace-with-a-768x768.webp 768w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.43.34-A-concluding-illustration-summarizing-best-practices-for-using-rest_api_init-in-WordPress-REST-API-development.-Show-a-professional-workspace-with-a-12x12.webp 12w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-29-11.43.34-A-concluding-illustration-summarizing-best-practices-for-using-rest_api_init-in-WordPress-REST-API-development.-Show-a-professional-workspace-with-a-96x96.webp 96w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><p>\u6b64\u5916\uff0c\u5c07 WPML \u548c Polylang \u7b49\u591a\u8a9e\u8a00\u5916\u639b\u7a0b\u5f0f\u6574\u5408\u81f3\u60a8\u7684\u81ea\u8a02 API \u9700\u8981\u7279\u5225\u6ce8\u610f\u7d30\u7bc0\uff0c\u4ee5\u78ba\u4fdd\u61c9\u7528\u6b63\u78ba\u7684\u8a9e\u8a00\u4e0a\u4e0b\u6587\u3002\u5728\u958b\u767c\u64f4\u5145 REST API \u7684\u81ea\u8a02 WordPress \u5916\u639b\u6642\uff0c\u9075\u5faa\u9019\u4e9b\u6700\u4f73\u5be6\u52d9\u8207\u7591\u96e3\u6392\u89e3\u6280\u5de7\u5c07\u53ef\u7bc0\u7701\u60a8\u7684\u6642\u9593\u8207\u9ebb\u7169\u3002<\/p><p>\u5982\u679c\u60a8\u4ecd\u5728\u7169\u60f1 <code>rest_api_init<\/code> \u8acb\u8003\u616e\u6709\u7cfb\u7d71\u5730\u505c\u7528\u5916\u639b\u7a0b\u5f0f\u3001\u4f7f\u7528 Postman \u6216 REST API Console \u7b49\u9664\u932f\u5de5\u5177\uff0c\u4e26\u78ba\u8a8d\u60a8\u7684\u74b0\u5883\u7b26\u5408 REST API \u958b\u767c\u7684\u6240\u6709\u5fc5\u8981\u689d\u4ef6\u3002<\/p><p>\u5982\u679c\u60a8\u5728 WordPress \u4e2d\u4f7f\u7528\u81ea\u8a02 REST API \u8def\u7531\u6642\u6709\u4efb\u4f55\u554f\u984c\u6216\u9700\u8981\u9032\u4e00\u6b65\u5354\u52a9\uff0c\u6b61\u8fce\u5728\u4e0b\u65b9\u7559\u8a00\u3002<\/p>","protected":false},"excerpt":{"rendered":"<p>\u5728\u60a8\u7684\u81ea\u8a02 WordPress \u5916\u639b\u4e2d\u9047\u5230 rest_api_init \u7121\u6cd5\u904b\u4f5c\u7684\u554f\u984c\u53ef\u80fd\u6703\u76f8\u7576\u4ee4\u4eba\u6cae\u55aa\uff0c\u5c24\u5176\u662f\u7576\u4e00\u5207\u4f3c\u4e4e\u90fd\u61c9\u8a72\u6b63\u78ba\u8a2d\u5b9a\u7684\u6642\u5019....\u3002<\/p>","protected":false},"author":31,"featured_media":4317,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,78],"tags":[375,376,220],"class_list":["post-4319","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-rest-api","category-rest-api-use","tag-rest-api","tag-rest_api_init","tag-wordpress",""],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.openbyt.com\/zh\/wp-json\/wp\/v2\/posts\/4319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.openbyt.com\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.openbyt.com\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.openbyt.com\/zh\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/www.openbyt.com\/zh\/wp-json\/wp\/v2\/comments?post=4319"}],"version-history":[{"count":1,"href":"https:\/\/www.openbyt.com\/zh\/wp-json\/wp\/v2\/posts\/4319\/revisions"}],"predecessor-version":[{"id":4332,"href":"https:\/\/www.openbyt.com\/zh\/wp-json\/wp\/v2\/posts\/4319\/revisions\/4332"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.openbyt.com\/zh\/wp-json\/wp\/v2\/media\/4317"}],"wp:attachment":[{"href":"https:\/\/www.openbyt.com\/zh\/wp-json\/wp\/v2\/media?parent=4319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.openbyt.com\/zh\/wp-json\/wp\/v2\/categories?post=4319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.openbyt.com\/zh\/wp-json\/wp\/v2\/tags?post=4319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}