Skip to content

Instantly share code, notes, and snippets.

@spyesx
Last active March 15, 2024 12:05
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save spyesx/561b1d65d4afb595f295 to your computer and use it in GitHub Desktop.
Save spyesx/561b1d65d4afb595f295 to your computer and use it in GitHub Desktop.
String to slug in JS (wordpress sanitize_title)
var string_to_slug = function (str)
{
str = str.replace(/^\s+|\s+$/g, ''); // trim
str = str.toLowerCase();
// remove accents, swap ñ for n, etc
var from = "àáäâèéëêìíïîòóöôùúüûñçěščřžýúůďťň·/_,:;";
var to = "aaaaeeeeiiiioooouuuuncescrzyuudtn------";
for (var i=0, l=from.length ; i<l ; i++)
{
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
}
str = str.replace('.', '-') // replace a dot by a dash
.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
.replace(/\s+/g, '-') // collapse whitespace and replace by a dash
.replace(/-+/g, '-') // collapse dashes
.replace( /\//g, '' ); // collapse all forward-slashes
return str;
}
@ryanhellyer
Copy link

AWESOME! Thanks so much for posting this here. I started to write something to do this, then thought "hmmm, I wonder if someone else has tried doing this before" and low and behold, here was your code in a Google search :)

@aguevara92
Copy link

Great resource, I just found an improvement. In Wordpress if you have a title with a dot (.) it will be replaced to a - in the slug. Currently, your solution doesn't do that. I just added that rule in the last part of your code:

str = str.replace('.', '-') // replace a dot for a - .replace(/[^a-z0-9 -]/g, '') // remove invalid chars .replace(/\s+/g, '-') // collapse whitespace and replace by - .replace(/-+/g, '-'); // collapse dashes

@spyesx
Copy link
Author

spyesx commented Jun 2, 2018

@ryanhellyer Glad it helped :) Thx

@aguevara92 Good idea. Gonna update it!

@DaveyJake
Copy link

Thank you for posting this!

Just FYI: Not sure if it matters but according to the codex, all / in strings are collapsed and not replaced by any character. I added .replace( /\//g, '' ) // collapse all forward-slashes by a dash to the block that starts at line 15.

@vyskoczilova
Copy link

Gread! Adding more chars from Czech alphabet:

var from = "àáäâèéëêìíïîòóöôùúüûñçěščřžýúůďťň·/_,:;";
var to   = "aaaaeeeeiiiioooouuuuncescrzyuudtn------";

@LiamBailey
Copy link

You should prototype this off String object, it would be handier to use - just a thought

@spyesx
Copy link
Author

spyesx commented Aug 24, 2019

@LiamBailey Just for you buddy ;)

String.prototype.toSlug = function ()
{
	var str = this;
	str = str.replace(/^\s+|\s+$/g, ''); // trim
	str = str.toLowerCase();

	// remove accents, swap ñ for n, etc
	var from = "àáäâèéëêìíïîòóöôùúüûñçěščřžýúůďťň·/_,:;";
	var to   = "aaaaeeeeiiiioooouuuuncescrzyuudtn------";

	for (var i=0, l=from.length ; i<l ; i++)
	{
		str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
	}

	str = str.replace('.', '-') // replace a dot by a dash 
		.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
		.replace(/\s+/g, '-') // collapse whitespace and replace by a dash
		.replace(/-+/g, '-') // collapse dashes
		.replace( /\//g, '' ); // collapse all forward-slashes

	return str;
}

@Loosie94
Copy link

Loosie94 commented Feb 19, 2021

Thanks a lot!

I've used this with Panini to get it working with {{#tim}} text {{/tim}} like so:

module.exports = function (options) {
    // options.fn(this) = Handelbars content between {{#trim}} HERE {{/trim}}
    var str = options.fn(this);
    str = str.replace(/^\s+|\s+$/g, ''); // trim
    str = str.toLowerCase();

    // remove accents, swap ñ for n, etc
    var from = "àáäâèéëêìíïîòóöôùúüûñçěščřžýúůďťň·/_,:;";
    var to = "aaaaeeeeiiiioooouuuuncescrzyuudtn------";

    for (var i = 0, l = from.length; i < l; i++) {
        str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
    }

    str = str.replace('.', '-') // replace a dot by a dash 
        .replace(/[^a-z0-9 -]/g, '') // remove invalid chars
        .replace(/\s+/g, '-') // collapse whitespace and replace by a dash
        .replace(/-+/g, '-') // collapse dashes
        .replace(/\//g, ''); // collapse all forward-slashes

    return str;
}

@spyesx
Copy link
Author

spyesx commented Apr 17, 2021

Looks like @salcode took it deeper to a new level :)

https://github.com/salcode/fe-sanitize-title-js

@salcode
Copy link

salcode commented Apr 20, 2021

@spyesx, as you mentioned, I did dive into this problem 😀

Ultimately, two things happened:

  1. I found emojis added a ton of complexity (and I never solved this in my code)
  2. I discovered the cleanForSlug() method of @wordpress/url (which is available in the context of the WordPress block editor (a.k.a. Gutenberg) at wp.url.cleanForSlug())

All that said, depending on your needs sometimes a function like you have here covers the use case you need. 👍

On a side note, one interesting thing I found out about WordPress sanitize_title() is that its behavior varies depending on the language being used (see salcode/fe-sanitize-title-js#1).

@cleissonbarbosa
Copy link

@passatgt
Copy link

passatgt commented Jan 6, 2024

You can also do wp.url.cleanForSlug('Test Title'). Works in the frontend too. I used it to generate a table of contents section in the sidebar.

            jQuery(document).ready(function($) {
                var headings = $('.entry-content .wp-block-heading');
                if(headings.length == 0) {
                    $('.faq-post-contents').hide();
                }
                headings.each(function(index, element) {
                    var text = $(element).text();
                    var slug = $(element).attr('id');

                    //Create slug if not exists
                    if(!slug) {
                        slug = wp.url.cleanForSlug(text);
                        $(element).attr('id', slug);
                    }

                    $('.faq-post-contents ul').append('<li><a href="#'+slug+'">'+text+'</a></li>');
                });

            });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment