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?
wp-json
as default wordpress api prefixcheck_user_authentication
is doing the work that apermission_callback
is intended to do, WordPress will be generating deprecation warnings in your error log due to the lack of apermission_callback
( it's not optional ). Have you tried flushing permalinks/rewrite rules?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