最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sanitization - Cannot get 'sanitize_callback' to work for rest parameters

programmeradmin1浏览0评论

I'm trying to sanitize my rest parameters using sanitize_callback:

register_rest_route( SoundSystem::$rest_namespace, '/playlist/new', array(
  'methods' => WP_REST_Server::CREATABLE,
  'callback' => array( __class__, 'rest_add_playlist' ),
  'permission_callback' => function () {
    return is_user_logged_in();
  },
  'playlist' => array(
    'description' => __( 'JSPF playlist data', 'soundsystem' ),
    'type'        => 'string',
    'required' => true,
    'sanitize_callback' => function($value, $request, $param) {
      return 'TESTING';
    },
  )
));

But it seems that the data is not sanitized: I get the initial value.

public static function rest_add_playlist(WP_REST_Request $request){
  $params = $request->get_params();
  $playlist = $params['playlist'];//does not returns 'TESTING'
}

What am I doing wrong ?

I'm trying to sanitize my rest parameters using sanitize_callback:

register_rest_route( SoundSystem::$rest_namespace, '/playlist/new', array(
  'methods' => WP_REST_Server::CREATABLE,
  'callback' => array( __class__, 'rest_add_playlist' ),
  'permission_callback' => function () {
    return is_user_logged_in();
  },
  'playlist' => array(
    'description' => __( 'JSPF playlist data', 'soundsystem' ),
    'type'        => 'string',
    'required' => true,
    'sanitize_callback' => function($value, $request, $param) {
      return 'TESTING';
    },
  )
));

But it seems that the data is not sanitized: I get the initial value.

public static function rest_add_playlist(WP_REST_Request $request){
  $params = $request->get_params();
  $playlist = $params['playlist'];//does not returns 'TESTING'
}

What am I doing wrong ?

Share Improve this question asked Feb 9, 2022 at 13:09 gordiegordie 4924 silver badges19 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

You're not getting the TESTING because your playlist argument should actually be in the args array like so: (reindented for brevity)

register_rest_route( SoundSystem::$rest_namespace, '/playlist/new', array(
    'methods'             => WP_REST_Server::CREATABLE,
    'callback'            => array( __class__, 'rest_add_playlist' ),
    'permission_callback' => function () {
        return is_user_logged_in();
    },
    'args'                => array(
        'playlist' => array(
            'description'       => __( 'JSPF playlist data', 'soundsystem' ),
            'type'              => 'string',
            'required'          => true,
            'sanitize_callback' => function($value, $request, $param) {
                return 'TESTING';
            },
        ),
    ), // end args
));
发布评论

评论列表(0)

  1. 暂无评论