• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

I Like Kill Nerds

The blog of Australian Front End / Aurelia Javascript Developer & brewing aficionado Dwayne Charrington // Aurelia.io Core Team member.

  • Home
  • Aurelia 2
  • Aurelia 1
  • About
  • Aurelia 2 Consulting/Freelance Work

Creating a Custom WordPress REST API Endpoint That Can Accept Spaces

Wordpress · October 12, 2021

The WordPress REST API is one of my favourite features in WordPress. The ability to create custom endpoints for getting data and other facets of building in WordPress makes life so much easier.

Recently, while building a custom endpoint that allowed for some custom post type content to be searched, I encountered a snag.

register_rest_route( 'raw-queue/v1', '/queue/(?P<name>[a-zA-Z0-9-]+)/(?P<results>[\d]+)/(?P<page>[\d]+)/(?P<source>[a-zA-Z0-9-]+)/(?P<term>([a-zA-Z])+)', array(
  'methods' => 'GET',
  'callback' => 'rest_get_news_stories_from_category',
  'permission_callback' => '__return_true'
) );

For singular search terms, it worked well. However, once I searched for words like “basketball player”, I would get the dreaded 404 error.

It turns out you need to tell WordPress in your regular expression path to allow the %20 character (which is a space). By default, spaces are ignored (as are other characters).

register_rest_route( 'raw-queue/v1', '/queue/(?P<name>[a-zA-Z0-9-]+)/(?P<results>[\d]+)/(?P<page>[\d]+)/(?P<source>[a-zA-Z0-9-]+)/(?P<term>([a-zA-Z]|%20)+)', array(
  'methods' => 'GET',
  'callback' => 'rest_get_news_stories_from_category',
  'permission_callback' => '__return_true'
) );

Notice the |%20 part in the route pattern? That tells WordPress we are okay with spaces in our route, and it will no longer break. Now, search terms like basketball%20player will work with our route. You can add in additional URL characters as you need them.

Now, last and most importantly, inside of your route callback, you need to make sure you use urldecode to remove the characters from the URL.

urldecode($request->get_param('term'));

Dwayne

Leave a Reply Cancel reply

0 Comments
Inline Feedbacks
View all comments

Primary Sidebar

Popular

  • Testing Event Listeners In Jest (Without Using A Library)
  • How To Get The Hash of A File In Node.js
  • Waiting for an Element to Exist With JavaScript
  • Thoughts on the Flipper Zero
  • How To Get Last 4 Digits of A Credit Card Number in Javascript
  • How To Paginate An Array In Javascript
  • Reliably waiting for network responses in Playwright
  • How To Mock uuid In Jest
  • How to Copy Files Using the Copy Webpack Plugin (without copying the entire folder structure)
  • Wild Natural Deodorant Review

Recent Comments

  • Dwayne on Is Asking Developers How to Write FizzBuzz Outdated?
  • kevmeister68 on Is Asking Developers How to Write FizzBuzz Outdated?
  • Kevmeister68 on Start-Ups and Companies That Embrace Work From Anywhere Will Be More Likely to Survive the Coming Recession in 2023
  • kevmeister68 on What Would Get People Back Into the Office?
  • Dwayne on PHP Will Not Die

Copyright © 2023 · Dwayne Charrington · Log in

wpDiscuz