0

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?

0

1 Answer 1

2

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',
3
  • Ok, I think there is something like that being done in a PR I submitted here. I rewrote everything using ES6 and webpack. Once this is merged I think this should solve the issues of wrong routes. But thanks for the heads up!
    – dingo_d
    Commented Dec 27, 2017 at 10:09
  • I try the branch you send with the pull request and I have an error : github.com/dingo-d/theme-sniffer/tree/feature/… the backend page doesn't found assets/build/scripts/application.js and when I load assets/dev/application.js I have the JavaScript error SyntaxError: import declarations may only appear at top level of a module I am not a JavaScript expert then I cannot help you to solve this.
    – mmm
    Commented Dec 27, 2017 at 11:32
  • The code needs to be built first ;) That's probably why it won't work. I will fix this when the PR is merged, and I'll release a plugin ready .zip file. Thanks!
    – dingo_d
    Commented Dec 27, 2017 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.