Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Limiting the number of words or characters in Wordpress slug/permalink. I would like to limit the number to the first 5 words (or 20 characters) even if the post title is longer.
example:
Title: Welcome to my site - this is my first post
url: mysite.com/welcome-to-my-site

Some function or alteration in the core Wordpress to achieve this result?

share|improve this question
    
you can edit the permalink when adding a post, but there probably is a way to automate this... –  janw May 20 '12 at 12:48

1 Answer 1

up vote 1 down vote accepted

You can probably do something with the sanitize_title hook based on the context conditionally, but I'm not familiar enough with where else sanitize_title is used to say for sure that this is a good solution. The trick to this is going to be limiting your slug without including stupid words that are going to hurt your SEO. As a launching point, try this:

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

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

    //  vars
    $desired_length = 20; //number of chars
    $desired_words = 5; //number of words
    $prohibited = array(
        'the'
        ,'in'
        ,'my'
        ,'etc'
        //put any more words you do not want to be in the slug in this array
    );

    //  do the actual work
    // filter out unwanted words
    $_title = explode( ' ', $title );
    //if you want more than one switch to preg_split()
    $_title = array_diff( $_title, $prohibited );
    // count letters and recombine
    $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;
}

Note that that is completely untested and I literally just wrote it, so it may have some kinks in it, but it's a good starting place for you.

share|improve this answer
1  
You should use mb_strlen() … and return something. A string would be useful. :) –  toscho May 20 '12 at 14:53
1  
@toscho You must have missed the last line, it returns $new_title. Thanks for the tip on strlen(), never knew that. –  m0r7if3r May 20 '12 at 14:57
2  
@MichaelWilliam The add_filter was missing the last argument, so WordPress didn't pass all three parameters to the callback. It is fixed now. –  toscho May 20 '12 at 16:48
2  
@MichaelWilliam See m0r7if3r's answer (and every other answer on our site) as a hint for a possible direction. Do not expect – and do not ask for – complete working code without your own research efforts. –  toscho May 20 '12 at 17:14
1  
@MichaelWilliam That's not my code, m0r7if3r has written it. :) –  toscho May 22 '12 at 1:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.