Search code examples
regexwordpresswordpress-rest-api

Regular expression length (0) or (7-50)


The regex here is using wp-rest-api enpoints regex so what I have is

$routes['/dn/settings/update/(?P<password>.{7,50})/(?P<newsletter>\d)'] = array(
    array( array( $this, 'settings_update'), WP_JSON_Server::ALLMETHODS ),
);

and the part I need help with is the (?P<password>.{7,50})

Right now it accepts 7-50 characters for the password but I also want the update to work if there is NO password submitted. ie. "/dn/settings/update//1"

How can I do something like .{0||7,50} while not using .{0, 50} ?


Solution

  • Just make your whole pattern optional:

    $routes['/dn/settings/update/(?P<password>.{7,50})?/(?P<newsletter>\d)'] = array(
        array( array( $this, 'settings_update'), WP_JSON_Server::ALLMETHODS ),
    );