Whenever you’re working with WordPress and third-party APIs, there’s always a chance that you’re going to run into issues where the third-party API is not prepared to handle certain characters that exist in a permalink.
Depending on one’s permalink settings, though, this may or may not be an issue. For example, you may have your permalinks set to something like ?p=123 for each post. In that case, it’s not much of an issue.
But if you’re using “pretty permalinks” and your permalink includes something like a trademark symbol or a copyright symbol, then it may cause problems whenever you’re communicating with said third-party API.
Remove Special Characters from Permalinks
First, if you’re using pretty permalinks, these are generally created from the post title. So if you use something such as All About The Acme Machine™ will generate a permalink that includes something like that.
For example, you may have WordPress automatically generate something like this: https://acme.com/all-about-the-acme®-machine/
And when it comes time to send that information to a third-party API (or to try to process it using anything that’s not prepared for such characters) then you’re going to run into trouble.
Fortunately, WordPress provides a hook that allow us to process permalinks before they are serialized to the database. Namely, this is the wp_insert_post_data hook.
And in this case, it works like this:
- Setup the hook to fire late in the serialization process,
- Look for characters that may be problematic,
- Replace them with an empty string,
- Return the updated information to WordPress for serialization.
Here’s an example that looks to remove various symbols such as copyright symbols, trademark symbols, registration symbols, and other variants thereof both in encoded formats and in the encoded equivalents:
Notice that the function accepts the post data as an argument, checks to make sure you’re not working with a draft, a pending article, or an auto-draft, and then removes contents in the post name by
This may be a nuanced issue. That is, perhaps you won’t encounter it in your development efforts. If, on the other hand, you do, then this provides a solution that not only allows you to remove certain characters from the URL, but also allows you to do so in a scalable.
So as your needs change, you can simply update the preg_replace call with the characters you need to strip from your URL.