How to Automatically Trim WordPress Post URL to Make it Readable and SEO Friendly | Tutorial Freak – Online Tutorials

How to Automatically Trim WordPress Post URL to Make it Readable and SEO Friendly

wordpress-logo

How to Automatically Trim WordPress Post URL to Make it Readable and SEO Friendly

In WordPress, Every post will be assigned with unique URL called “Slug”. If you didn’t manually edit it, Slug is automatically assigned to your post depending on the permalinks settings and post title.

Mostly we all are in so much of a rush that we forget to check Slug before publishing, So words like a, in, or, and, from your post title makes the url longer, unreadable and less SEO friendly. Things go much worse when your post title is really longer..

According to Matt Cutts (Popular figure in SEO industry), Google bots won’t be having problem reading shorter and descriptive urls while longer urls makes it less SEO friendly because Google will give more weight to keywords at the beginning.

If you have got a three, four or five words in your URL, that can be perfectly normal. As it gets a little longer, then it starts to look a little worse. Now, our algorithms typically will just weight those words less and just not give you as much credit.

So how to make post URL short and SEO Friendly in an Automatic way. Yes, Codes.

Method 1

Lets say you like to remove all words which has less than three letters, Such as a, an, or, in etc. Add the following code at the end of your current theme’s functions.php file

add_filter(‘sanitize_title’, ‘remove_short_words’);
function remove_short_words($slug) {
if (!is_admin()) return $slug;
$slug = explode(‘-’, $slug);
foreach ($slug as $k => $word) {
if (strlen($word) < 3) { unset($slug[$k]); } } return implode('-', $slug); }

If you like to go more aggressive like eliminating all three letter words then just change the line 6 of the above code with desired letter count (increase it by one).

if (strlen($word) < 4) {

Method 2

Previously we eliminated words according to its letter count but you can do it more efficiently by adding a dictionary which contains list of words to eliminate from URL. Add the following code to functions.php file as you did earlier:

add_filter(‘sanitize_title’, ‘remove_false_words’);
function remove_false_words($slug) {
if (!is_admin()) return $slug;
$slug = explode(‘-’, $slug);
foreach ($slug as $k => $word) {
//false words list separated for commas
$keys_false = ‘a,about,above,across,after,again,against,all,almost,alone,along,already,also’;
$keys = explode(‘,’, $keys_false);
foreach ($keys as $l => $wordfalse) {
if ($word==$wordfalse) {
unset($slug[$k]);
}
}
}
return implode(‘-’, $slug);
}

In line 7 of the above code you can find the list of words which are blacklisted. You can add more words by separating each words with a comma. For example lets add words like and and in to the list

$keys_false = ‘a,about,above,across,after,again,against,all,almost,alone,along,already,also,and,in’;

This method is bit safer and more efficient because words like “against”, “already” contain more than five letters and if you decide to eliminate all five letter words then you may end up eliminating keywords as well. Its little bit of a pain in the ass to add those words but its worth the effort.

KeithM..

Pro Photographer, WebMaster & Writer.. Please follow us on Facebook

KeithM.. – who has written posts on Tutorial Freak – Online Tutorials.


Help keep us alive & kicking, we need coffee 24/7, please donate, thanks!
Embed

Leave a Reply