{"id":4854,"date":"2025-04-09T18:36:19","date_gmt":"2025-04-09T17:36:19","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=4854"},"modified":"2025-10-06T21:55:28","modified_gmt":"2025-10-06T20:55:28","slug":"adding-custom-endpoints","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/wordpress\/pages-wordpress\/adding-custom-endpoints","title":{"rendered":"Adding Custom Endpoints"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Create a simple GET endpoint<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>add_action( 'rest_api_init', function () {\n  register_rest_route( 'my_endpoint', '\/do_something', array(\n    'methods' => 'GET',\n    'callback' => '\\MyNamespace\\my_endpoint_callback',\n    'permission_callback' => '__return_true'                \/\/Use this for a public endpoint\n  ) );\n} );\n\nfunction my_endpoint_callback ($Request = null)\n{\n  $Name = \"\";\n  if (isset($_REQUEST&#91;'name']))\n    $Name= $_REQUEST&#91;'name'];\n\n  $Output = array();\n  $Output&#91;'Result'] = \"Hi there: $Name\";\n\n  echo json_encode($Output);\n}\n\/\/You can use these URLs to access it (either, both do same thing):\n\/\/https:\/\/my_domain.com\/?rest_route=\/my_endpoint\/do_something&amp;name=John\n\/\/https:\/\/my_domain.com\/wp-json\/my_endpoint\/do_something&amp;name=John<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Create a simple POST endpoint<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>add_action( 'rest_api_init', function () {\n  register_rest_route( 'my_endpoint', '\/do_something', array(\n    'methods' =&gt; 'POST',\n    'callback' =&gt; '\\MyNamespace\\my_endpoint_callback',\n    'permission_callback' =&gt; '__return_true'\n  ) );\n} );\n\nfunction my_endpoint_callback ($Request = null)\n{\n  $Parameters = $Request-&gt;get_params();     \/\/get_params() returns all parameters for the request as an array\n  \/\/var_dump($Parameters);\n\n  $SomeFieldValue = '';\n  if (isset($Parameters&#91;'some_field']))\n    $SomeFieldValue = sanitize_text_field($Parameters&#91;'some_field']);\n\n  $Output = array();\n  $Output&#91;'Result'] = \"success\";\n\n  echo json_encode($Output);\n}\n\/\/You can use these URLs to access it (either, both do same thing):\n\/\/https:\/\/my_domain.com\/?rest_route=\/my_endpoint\/do_something\n\/\/https:\/\/my_domain.com\/wp-json\/my_endpoint\/do_something<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Authenticating the current user in a call to the endpoint<\/h4>\n\n\n\n<p>A REST api request is by default non-authenticated. If you want to use things like get_current_user_id() in your endpoint handling code, you need to pass a nonce to the endpoint when calling it.<\/p>\n\n\n\n<p>If using POST then ensure you include a POST parameter named &#8216;_wpnonce&#8217;.<\/p>\n\n\n\n<p>The value to put in it can be obtained using:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $WpNonce = wp_create_nonce('wp_rest');      \/\/Create a nonce for our REST api request<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Debugging a POST endpoint<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/To debug this endpoint: \n\/*\n  $WpNonce = wp_create_nonce('wp_rest');      \/\/Create a nonce for our REST api request\n  $HtmlOutput = &lt;&lt;&lt;_END\n    &lt;form action=\"\/?rest_route=\/my_endpoint\/do_something\" method=\"POST\"&gt;\n      &lt;input type=\"hidden\" name=\"_wpnonce\" value=\"$WpNonce\" \/&gt;\n      \n      &lt;label&gt;A value you need to pass:&lt;\/label&gt;\n      &lt;input type=\"text\" name=\"MyValueName\" style=\"width:200px;\" value=\"\"&gt;\n      \n      &lt;input type=\"submit\" value=\"Test Endpoint\" \/&gt;\n    &lt;\/form&gt;\n_END;\n  echo $HtmlOutput\n*\/<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Create a simple GET endpoint Create a simple POST endpoint Authenticating the current user in a call to the endpoint A REST api request is by default non-authenticated. If you want to use things like get_current_user_id() in your endpoint handling code, you need to pass a nonce to the endpoint when calling it. If using [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[369,346,120,164],"tags":[],"class_list":["post-4854","post","type-post","status-publish","format-standard","hentry","category-endpoints","category-forms-wordpress","category-pages-wordpress","category-plugin-api"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=4854"}],"version-history":[{"count":12,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4854\/revisions"}],"predecessor-version":[{"id":5157,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4854\/revisions\/5157"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=4854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=4854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=4854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}