0

I have this code inside plugin I'm writing. I need to add some filters to customize WP REST API anyway I'm noticing that filters seems not working

if( !defined( 'ABSPATH' ) ) {
    exit;
}

class Microservices {
    public function __construct()
    {
        add_filter( 'rest_url_prefix', array( $this, 'set_rest_api_prefix' ) );
        //add_filter( 'rest_authentication_errors', array( $this, 'check_user_authentication' ) );
        //
        //add_action( 'rest_api_init', array( $this, 'extend_default_routes' ) );
        //add_action( 'rest_api_init', array( $this, 'extend_default_rest_fields' ) );
    }

    /*
    *
    */
    public function set_rest_api_prefix()
    {
        return 'api';
    }

    /*
    *
    */
    public function extend_default_routes() 
    {
        //register_rest_route( 
        //    'extranet/', 
        //    'v1/login', 
        //    array(
        //        'methods' => 'POST'
        //    ), 
        //    true 
        //);
        register_rest_route( 
            'extranet/', 
            'v1/userDetails', 
            array(
                'methods' => 'GET',
                'callback' => array( $this, 'get_logged_user_info' )
            ), 
            false 
        );
        //
        register_rest_route( 
            'extranet/', 
            'v1/sendComunication', 
            array(
                'methods' => 'POST',
                'callback' => array( $this, 'send_new_comunication' )
            ), 
            false 
        );
        //
        register_rest_route(
            'extranet/',
            'v1/loadArchive',
            array(
                'methods' => 'GET',
                'callback' => array( $this, 'load_files_archive' )
            )
        );
        //
        register_rest_route( 
            'extranet/', 
            'v1/createFolder', 
            array(
                'methods' => 'POST',
                'callback' => array( $this, 'create_new_folder' )
            ), 
            false 
        );
        //
        register_rest_route(
            'extranet/',
            'v1/uploadFiles',
            array(
                'methods' => 'POST',
                'callback' => array( $this, 'upload_new_files' )
            ),
            false
        );

    } 
    
    /*
    *
    */
    public function extend_default_rest_fields()
    {
        //register_rest_field( 
        //    $object_type:string|array, 
        //    $attribute:string, 
        //    $args:array 
        //);
    }

    /*
    *
    */
    public function check_user_authentication( $result ) 
    {
        // If a previous authentication check was applied,
        // pass that result along without modification.
        if( true === $result || is_wp_error( $result ) ) {
            return $result;
        }

        // No authentication has been performed yet.
        // Return an error if user is not logged in.
        if( !is_user_logged_in() ) {
            return new WP_Error(
                'rest_not_logged_in',
                __( 'You are not currently logged in.' ),
                array( 'status' => 403 )
            );
        }

        // Our custom authentication check should have no effect
        // on logged-in requests
        return $result;
    }
}

new Microservices();

I've tried to call the renamed api from postman in my dev machine using localhost, but it will result in a 404, but the wp-json still work. The plugin is active, so what's the problem with the code?

7
  • Is this code in the main plugin file? Or included separately in some way? Aug 31 at 12:06
  • It's in the èlugin file. I've noticed that I had a typo into the __constructor, but I'm still unable to remove wp-json as default wordpress api prefix
    – ICTDEV
    Aug 31 at 12:14
  • Your check_user_authentication is doing the work that a permission_callback is intended to do, WordPress will be generating deprecation warnings in your error log due to the lack of a permission_callback ( it's not optional ). Have you tried flushing permalinks/rewrite rules?
    – Tom J Nowell
    Aug 31 at 12:42
  • @TomJNowell not tested yet, I've done the flushing permalink operation
    – ICTDEV
    Aug 31 at 12:47
  • @ICTDEV I see extend_default_routes is not called, the filter that adds it is commented out, I also see the __contruct vs __construct mistake is still present in the code
    – Tom J Nowell
    Aug 31 at 15:29

1 Answer 1

1

This is a problem:

public function __contruct()

it should be __construct, the S is missing, meaning there is no constructor and the add_filter calls never happen

The function that registers the routes also never runs because the add_action call is commented out:

//add_action( 'rest_api_init', array( $this, 'extend_default_routes' ) );

This means the constructor never runs, and if it did run the extend_default_routes function would never be called, so the routes are never registered.

3
  • I've solved that typo, but if I try to reach my custom endpoint using localhost/sits/api/extranet/v1/loadArchive for example, also if I disable the authentication check, I will get a 404 from postman
    – ICTDEV
    Aug 31 at 13:17
  • did you flush permalinks too? i'ma ssuming /sits/ is where your WordPress install is? Have you tried adding a permission_callback and setting it to __return_true then testing this without the additional authentication filter that was added? There's not much point validating an authenticated request if we can't confirm the endpoint exists to begin with. It also looks like you commented out the filters that add your endpoints. Can you update your question with the latest code if you've made fixes?
    – Tom J Nowell
    Aug 31 at 13:41
  • I've updated the code to disable the authentication check. And yes, sits is the folder under localhost where I've installed wordpress
    – ICTDEV
    Aug 31 at 13:49

Your Answer

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

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