Skip to main content
edited body
Source Link
mor7ifer
  • 8.6k
  • 2
  • 19
  • 32

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 != 10 )
            $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.

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 != 1 )
            $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.

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.

made sure 3 args are passed
Source Link
fuxia
  • 107.1k
  • 38
  • 255
  • 459

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 != 1 )
            $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.

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 );

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 != 1 )
            $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.

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 != 1 )
            $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.

added 3 characters in body
Source Link
mor7ifer
  • 8.6k
  • 2
  • 19
  • 32

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 );

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( strlenmb_strlen( $new_title.' '.$_title[$i] ) > $desired_length )
            break;
        
        if( $i != 1 )
            $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.

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 );

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( strlen( $new_title.' '.$_title[$i] ) > $desired_length )
            break;
        
        if( $i != 1 )
            $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.

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 );

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 != 1 )
            $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.

Source Link
mor7ifer
  • 8.6k
  • 2
  • 19
  • 32
Loading