0

By default WordPress use a + between spaces whenever we search for something on a blog which is built on WordPress. Is there any way to change that + into a hyphen.

1

By default WordPress use a + between spaces

No, it doesn't. WordPress doesn't generate + characters, it doesn't use those characters, and it can't see them.

So Where Does the + Come From?

The browser and URLs. Spaces in URL parameters are encoded as + characters or %20. Forms specifically become +.

When data that has been entered into HTML forms is submitted, the form field names and values are encoded and sent to the server in an HTTP request message using method GET or POST, or, historically, via email.[3] The encoding used by default is based on an early version of the general URI percent-encoding rules,[4] with a number of modifications such as newline normalization and replacing spaces with + instead of %20. The media type of data encoded this way is application/x-www-form-urlencoded, and it is currently defined in the HTML and XForms specifications. In addition, the CGI specification contains rules for how web servers decode data of this type and make it available to applications.

https://en.wikipedia.org/wiki/Percent-encoding#The_application/x-www-form-urlencoded_type

Search URLs convert "test data" to test+data because that's how URL parameters generated by HTML forms are meant to work.

But Can It Be Done?

Is there any way to change that + into a hyphen.

Not without causing major problems, and unexpected bugs. To do this you would need to intercept the form inputs and replace spaces in the input with a - using javascript.

Then, you would need to intercept it on the PHP side and undo the change. As a result it will be impossible to search for things that contain hyphens. This will also break search functionality in a lot of themes and plugins.

This would also break most search plugins, and interfere with SEO plugins, which will interpret the search query as a single word with hyphens.

2
  • You mean HTML forms are causing this problem – Aman Rawat Feb 28 at 2:48
  • I'm saying this is how forms are supposed to work, it is not a bug, and trying to change it is a really bad idea that will require a lot of effort and cause a lot of problems – Tom J Nowell Feb 28 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.