Encoding Method for URLs?
-
16-10-2019 - |
Question
Is there a method or API that WordPress uses to encode the URLs similar to how it generates part of the URL when using the title in the URL? I am writing a plugin that generates URLs and would like to use the same method as everything else is.
For instance, I type "This is my blog post" in the title and "this-is-my-blog-post" gets generated.
Solution
On a lower level, the function sanitize_title_with_dashes()
converts to lowercase, replaces spaces and non-alphanumeric characters with dashes, and urlencodes whatever you pass to it.
<?php
echo sanitize_title_with_dashes( 'This is my blog post' );
// this-is-my-blog-post
OTHER TIPS
Post slugs seem to be generated by wp_unique_post_slug()
function (source).
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange