How to Limit Permalink Length in WordPress without any Plugin

Permalink

website url is Permalink. (like – www.thekroyaard.com/post-url)

How to Set Limit

Open your website’s theme and paste given code in functions.php file and SAVE.

add_filter( 'sanitize_title', 'thekroyaardspl_permalink_limit', 1, 3 );

function thekroyaardspl_permalink_limit( $title, $raw_title, $context ) {
// filters
if( $context != 'save' )
return $title;

// vars
$desired_length = 25; //number of chars
$desired_words = 6; //number of words

// do the actual work
// filter out unwanted words
$_title = explode( ' ', $title );
//if you want more than one switch to preg_split()
$new_title = '';
for( $i=0, $count=count($_title); $i<$count; $i++ ) {
//check for number of words
if( $i > $desired_words )
break;
//check for number of letters
if( mb_strlen( $new_title.' '.$_title[$i] ) > $desired_length )
break;

if( $i != 0 )
$new_title .= ' ';
$new_title .= $_title[$i];
}

return $new_title;
}

Hope this code is works in your website.

Video