I am stuck trying to register a REST route for a custom plugin function, to make that function callable by javascript.
Examples show endpoint functions embedded in the registration script in functions.php (such as at https://www.sitepoint.com/creating-custom-endpoints-for-the-wordpress-rest-api/)
However, my endpoint function is already in a PHP file in a custom plugin tree.
So I am trying to:
Register a route for my endpoint, for example:
www.example.com/test/v1/route_function_name
Point it to my custom function name, for example
name_of_function
Tell it where to find that function, for example
www.example.com/wp-content/plugins/custom-plugin-name/path-tree-stuff/public/plugin-functions-controller.php
I'm getting stuck at number 3. Issues are:
a) Where do I put this function name reference? I don't want to rip my function code out of that file and pasted it into the route registration code.
b) Where do I put the function file's path referen? Examples I have followed just have references to "array, this" instead of actual paths, so don't indicate how to register a route to an externally stored endpoint that is not encapsulated in the registration code.
c) Am I missing something? It looks like shielding the actual functions php file path from the JS will just result in exposing it in the WP functions file instead?
The dev handbook at https://developer.wordpress.org/rest-api/extending-the-rest-api/routes-and-endpoints/ looks like its examples are trying to hook wordpress functions via a custom route, not custom functions.
Are there examples anywhere that better suit my situation? (Where I have a custom function in a custom file in a custom plugin, and just need to register a REST route and point it to the existing function in the existing file).
So far I have the following which is obviously incomplete:
add_action(
'rest_api_init',
function () {
register_rest_route(
'test/v1',
'/route_function_name',
[
'methods' => 'POST',
'callback' => function (WP_REST_Request $request) {
// Need to somehow insert here a path to my function
name_of_function;
// Reponse is sent back by the function, so need to trap it [
'return-param-1' => $return-param-1,
'return-param-2' => $return-param-2,
'return-param-3' => $return-param-3,
];
},
]
);
}
);
Rationale for trying to do this is:
- I already have a JS script running
- I need it to call a PHP function I have already written
- This is easy: just code the function's path into the JS
- However I am trying to do it the proper WP way instead (chiefly for security reasons but also to appease the DevProperly spirits!)
I have assumed:
i) The place to register the REST route is in functions.php in the active child theme;
ii) The place for my custom function is in a php file inside my existing custom plugin;
iii) The code that registers the rest route needs to specify at least all of the following three things:
- The URI of the new REST route
- The name of the php function to be invoked
- The file and path of where that php function can be found within the custom plugin path, relative to /var/www
Are any of these assumptions incorrect?