{"id":2689,"date":"2020-04-02T19:24:13","date_gmt":"2020-04-02T18:24:13","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=2689"},"modified":"2026-02-06T14:34:47","modified_gmt":"2026-02-06T14:34:47","slug":"creating-an-api-url","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/wordpress\/pages-wordpress\/creating-an-api-url","title":{"rendered":"Creating an API URL"},"content":{"rendered":"\n<p>You may want a url to be used for say remote devices to connect to your site and pass information using say URL parameters and a response string.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">REST API method<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/*************************************\n\/\/*************************************\n\/\/********** MY API ENDPOINT **********\n\/\/*************************************\n\/\/*************************************\nadd_action( 'rest_api_init', function () {\n  register_rest_route( 'my-api-endpoint-subname', '\/my-api-endpoint-name', array(\n    'methods' => 'POST',\n    'callback' => '\\MyNamespace\\odac_my_api_endpoint_callback',\n    'permission_callback' => '__return_true'\n  ) );\n} );\n\/\/You can use these URLs to access it (either, both do same thing):\n\/\/Endpoint URL: https:\/\/thisdomain.com\/?rest_route=\/my-api-endpoint-subname\/my-api-endpoint-name\n\/\/Endpoint URL: https:\/\/thisdomain.com\/wp-json\/my-api-endpoint-subname\/my-api-endpoint-name\n\n\n\/\/To debug this endpoint: \n\/*\n  $WpNonce = wp_create_nonce('wp_rest');      \/\/Create a nonce for our REST api request\n  $HomeUrl = home_url();\n  $HtmlOutput = &lt;&lt;&lt;_END\n    &lt;form action=\"$HomeUrl\/?rest_route=\/my-api-endpoint-subname\/my-api-endpoint-name\" method=\"POST\">\n      &lt;input type=\"hidden\" name=\"_wpnonce\" value=\"$WpNonce\" \/>\n      \n      &lt;label>My Field 1:&lt;\/label>\n      &lt;input type=\"number\" name=\"MyField1\" style=\"width:100px;\" value=\"\">\n      \n      &lt;input type=\"submit\" value=\"Test Endpoint\" \/>\n    &lt;\/form>\n_END;\n  echo $HtmlOutput;\n*\/\nfunction odac_my_api_endpoint_callback ($Request = null)\n{\n\n\n\ttry\n\t{\n\n    \/\/----- GET POST PARAMETERS -----\n    \/\/$Response= array();\n    $Parameters = $Request->get_params();\n    $MyField1 = (isset($_POST&#91;'MyField1'])) ? trim($_POST&#91;'MyField1']) : \"\";\n\n    $MyReturnValue = \"abcd\";\n\n    echo json_encode(array('MyReturnValue' => $MyReturnValue));\n\t}\n\tcatch (\\Exception $e)\n\t{\n    echo json_encode(array('ErrorMessage' => 'Error: ' . $e->getMessage()));\n\t}\n\tcatch (\\Error $e)\n\t{\n\t\techo json_encode(array('ErrorMessage' => 'Error: ' . $e->getMessage()));\n\t}\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Page method<\/h4>\n\n\n\n<p>You can create a special url and trap it using the code below.  However for it to work in all instances you need to have a real wordpress page created with the url.  Otherwise when using wget under linux for example, you can get a 404 not found error returned<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/*************************************************\n\/\/*************************************************\n\/\/********** TEMPLATE PAGE ABOUT TO LOAD **********\n\/\/*************************************************\n\/\/*************************************************\n\/\/Hook executes just before WordPress determines which template page to load. It is a good hook to use if you need to do a redirect with full knowledge of the\n\/\/content that has been queried.\nadd_action(\"template_redirect\", 'mysite_template_redirect');\nfunction mysite_template_redirect()\n{\n  \n  \/\/----------------------------\n  \/\/----- OUR API URL PAGE -----\n  \/\/----------------------------  \n  \/\/URL to access this api function: \"mysitename.com\/api-v1\"\n  if (strpos($_SERVER&#91;\"REQUEST_URI\"], '\/api-v1') !== false)     \/\/&lt;&lt;&lt;There must be a real worldpress page for this url otherwise using wget from linux will not work and give a 404 not found error\n  {\n    \/\/GET URL ARGUMENTS\n    $AuthCode = \"\";\n    if (isset($_REQUEST&#91;'auth']))\n      $AuthCode = $_REQUEST&#91;'auth'];\n\n      die(\"OK-Auth-\" . $AuthCode);    \/\/&lt;&lt;&lt;&lt;This string is returned as the output to the device taht has used this url\n  }\n\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You may want a url to be used for say remote devices to connect to your site and pass information using say URL parameters and a response string. REST API method Page method You can create a special url and trap it using the code below. However for it to work in all instances you [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[180,369,120],"tags":[],"class_list":["post-2689","post","type-post","status-publish","format-standard","hentry","category-ajax-wordpress","category-endpoints","category-pages-wordpress"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2689","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\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=2689"}],"version-history":[{"count":5,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2689\/revisions"}],"predecessor-version":[{"id":5279,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2689\/revisions\/5279"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=2689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=2689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=2689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}