Wp Rest Api 自定义终点
Wp Rest Api Custom End point
我正在尝试将自定义端点添加到我的 wp-rest api 最新版本。我已经有了这个但是最后带有 slug 参数的那个不起作用..有谁知道为什么..如果有人能帮忙就太好了..
register_rest_route( 'wp/v2', '/guestmix', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_guestmixes' )
),
'schema' => array( $this, 'get_public_item_schema' )
) );
register_rest_route( 'wp/v2', '/guestmix/(?P<slug>\d+)', array(
'methods' => 'GET',
'callback' => 'get_guestmix'
) );
我猜是因为您对正则表达式使用了 d
元字符 (?P<slug>\d+)
表示数字,请尝试改用 S
。
代码应如下所示
register_rest_route( 'wp/v2', '/guestmix/(?P<slug>\S+)', array(
'methods' => 'GET',
'callback' => 'get_guestmix'
) );
这是作弊sheet供参考http://www.phpliveregex.com/
上面的答案对我有用,尽管我按照 2019 年的要点实现了略有不同的正则表达式,涵盖了不同的 url/slug-structure 场景。
register_rest_route( 'wp/v2', '/guestmix/(?P<slug>[a-zA-Z0-9-]+)', array(
'methods' => 'GET',
'callback' => 'get_guestmix'
) );
希望对您有所帮助
我正在尝试将自定义端点添加到我的 wp-rest api 最新版本。我已经有了这个但是最后带有 slug 参数的那个不起作用..有谁知道为什么..如果有人能帮忙就太好了..
register_rest_route( 'wp/v2', '/guestmix', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_guestmixes' )
),
'schema' => array( $this, 'get_public_item_schema' )
) );
register_rest_route( 'wp/v2', '/guestmix/(?P<slug>\d+)', array(
'methods' => 'GET',
'callback' => 'get_guestmix'
) );
我猜是因为您对正则表达式使用了 d
元字符 (?P<slug>\d+)
表示数字,请尝试改用 S
。
代码应如下所示
register_rest_route( 'wp/v2', '/guestmix/(?P<slug>\S+)', array(
'methods' => 'GET',
'callback' => 'get_guestmix'
) );
这是作弊sheet供参考http://www.phpliveregex.com/
上面的答案对我有用,尽管我按照 2019 年的要点实现了略有不同的正则表达式,涵盖了不同的 url/slug-structure 场景。
register_rest_route( 'wp/v2', '/guestmix/(?P<slug>[a-zA-Z0-9-]+)', array(
'methods' => 'GET',
'callback' => 'get_guestmix'
) );
希望对您有所帮助