Question

This question is basically a copy from the trac ticket I created #42973.

On the Theme Sniffer plugin I replaced admin-ajax.php with REST endpoints.

So when you want to run a check against the theme the route called to check your theme is something like

https://example.com/wp-json/theme-sniffer/v1/sniff-run

Now if I install WordPress in a subdirectory that's called /test the route should be

https://example.com/test/wp-json/theme-sniffer/v1/sniff-run

But that doesn't happen. The sniff run will try to access endpoint at https://example.com/wp-json/theme-sniffer/v1/sniff-run which doesn't exist!

The routes are added like this:

<?php
add_action( 'rest_api_init', 'theme_sniffer_endpoint_init' );
/**
 * Register endpoints function
 *
 * @since 0.1.0
 */
function theme_sniffer_endpoint_init() {
        register_rest_route( 'theme-sniffer/v1', '/sniff-run', array(
                'methods'  => 'POST',
                'callback' => 'theme_sniffer_run_sniffer',
        ) );
        register_rest_route( 'theme-sniffer/v1', '/individual-sniff', array(
                'methods'  => 'POST',
                'callback' => 'theme_sniffer_individual_sniff',
        ) );
}

You can check the code here.

My guess is that if the plugin namespace isn't defined, the subdirectory will be ignored, and it will assume that the endpoint originates from the root folder.

I haven't looked in the detail at the REST architecture in WP (will do later in the week), but if that is the case, will the namespacing the plugin help?

Was it helpful?

Solution

the URL is not good in the AJAX call

to correct that, modify the function theme_sniffer_admin_scripts in inc/admin.php to add the home URL to wp_localize_script :

"homeURL" => home_url(),

and then use this URL in the 2 AJAX calls in js/admin.js :

url: localizationObject["homeURL"] + '/wp-json/theme-sniffer/v1/sniff-run',
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top