Home Plugin documentation Javascript API pip.sanitize_title()

 

 

Here we go!

 

pip.sanitize_title()

Heads up!

This article contains Javascript code and is intended for developers. We offer this code as a courtesy, but don’t provide support for code customizations or 3rd party development.

Javascript equivalent of PHP’s sanitize_title()

/**
	* Sanitize value like WP function "sanitize_title"
	*
	* @param $val
	*
	* @returns {string}
	*/
pip.sanitize_title = function ( $val ) {
				return $val
								.toLowerCase()
								.replace( /\s+/g, '-' )                // Replace spaces with -
								.replace( /\-\-+/g, '-' )              // Replace multiple - with single -
								.replace( /\_\_+/g, '_' )              // Replace multiple _ with single _
								.replace( /^-+/, '' )                  // Trim - from start of text
								.normalize( 'NFD' )                    // Change accent to unicode value
								.replace( /[\u0300-\u036f]/g, '' )     // From unicode value to letter
								.replace( /[^a-zA-Z0-9_\-\s]+/g, '' ); // Remove all non-word chars
};

// To use it
let example_string = pip.sanitize_title( 'Example string' );
// example_string = "examplestring"

You didn’t find the answer you were looking for?

or you think a specific topic deserve more informations?

 

Wave to bottom