Limit the size of the existing and new permalink slugs in Wordpress for SEO

0 votes

 read an article on Google that says for good SEO it is best that the size of the slug in the URL is limited to 5 words.

As I use WordPress the link is assigned automatically to the article title. To redo all the links with just 5 words I would have to spend months editing all the links on my blog.

Would it be possible to do this automatically? There is some function or code for that. I found this code and added it to the function page of my theme and had no results.

See the code:

function pm_limit_slugs_length($uri) {
    $max_words = 5; // If any part of URI contains more than 5 words, the slug will be limited to first 5 words
    $new_title = '';
    $slugs = explode('/', $uri);
    
    for($i=0, $count = count($slugs); $i < $count; $i++) {
        $slug = $slugs[$i];
        $words = explode('-', $slug);
        
        $new_title .= "/";
        if(count($words) > $max_words) {
            $new_title .= implode("-", array_slice($words, 0, $max_words));
        } else {
            $new_title .= $slug;
        }
    }
    
    // Remove trailing slashes
    $new_title = trim($new_title, "/");
    
    return $new_title;
}
add_filter('permalink_manager_filter_default_post_uri', 'pm_limit_slugs_length', 99);
add_filter('permalink_manager_filter_default_term_uri', 'pm_limit_slugs_length', 99);

I found the code here: https://permalinkmanager.pro/docs/filters-hooks/how-to-limit-the-number-of-words-in-wordpress-permalinks/

How can I use it to limit the Wordpress slug size to 5 words?

Feb 24, 2022 in Others by Kichu
• 19,050 points
1,592 views

1 answer to this question.

0 votes
?php  
/**
 * Trim native slugs
 */
function pm_trim_native_slug($slug, $post_ID, $post_status, $post_type, $post_parent) {
    global $wpdb;

    $max_words = 5; // Limit the number of words to 5; This value can be changed.
    $words = explode('-', $slug);

    /* UPDATED CODE TO REMOVE SHORT WORDS */
    $min_word_length = 2;

    foreach ($words as $k => $word) {
        if (strlen($word) <= $min_word_length)
            unset($words[$k]);
    }
    /* END OF UPDATED CODE FOR SHORT WORDS */

    if(count($words) > $max_words) {
        $slug = implode("-", array_slice($words, 0, $max_words));

        // Make the slugs unique
        $check_sql       = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1";
        $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug, $post_ID));

        if($post_name_check) {
            $suffix = 2;
            do {
                $alt_post_name = _truncate_post_slug($slug, 200 - (strlen($suffix) + 1)) . "-$suffix";
                $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_post_name, $post_type, $post_ID, $post_parent));
                $suffix++;
            } while ($post_name_check);
            $slug = $alt_post_name;
        }
    }

    return $slug;
}
add_filter('wp_unique_post_slug', 'pm_trim_native_slug', 99, 5);

this code is to automatically remove short words from url
after this update the eisting slugs

<?php
    include('wp-load.php');           //Include the wp-load.php file
    define('WP_USE_THEMES', false);   //We don't need the theme files

    echo "<p>About to update all slugs...</p>";
    limit_all_existing_slugs();
    echo "<p>...DONE</p>";
?>

call this function to update the slugs
answered Feb 25, 2022 by narikkadan
• 63,420 points

Related Questions In Others

0 votes
1 answer

Excel VBA: Obtain the Column and Row of current element in "For Each" loop

Try this: MsgBox rng.Address(RowAbsolute:=False, ColumnAbsolute:=F ...READ MORE

answered Feb 14, 2023 in Others by Kithuzzz
• 38,010 points
779 views
0 votes
1 answer

Where is the documentation to refer for coinbase api integration of Etherium coin currency in php?

Hey there! Please refer to the following ...READ MORE

answered Jan 25, 2019 in Others by Omkar
• 69,210 points
526 views
0 votes
0 answers

In 2019, AtoX leads the new trend of decentralized exchanges

Under the influence of the blockchain continuing ...READ MORE

Jan 29, 2019 in Others by anonymous
332 views
–1 vote
0 answers
0 votes
1 answer

Simple question regarding PHP, Wordpress & Yoast SEO

the way the search engine displays your ...READ MORE

answered Feb 14, 2022 in Others by narikkadan
• 63,420 points
327 views
0 votes
0 answers

SEOPress plugin not working with Wordpress 5.7

An error of type E_ERROR was caused ...READ MORE

Feb 14, 2022 in Others by Kichu
• 19,050 points
717 views
0 votes
1 answer

Wordpress Critical Error Due to Plugin Conflict with my Functions.php

// WooCommerce Stock message add_filter( 'woocommerce_get_availability', 'mw_get_availability', ...READ MORE

answered Feb 26, 2022 in Others by narikkadan
• 63,420 points
923 views
0 votes
1 answer

Wordpress seo silo category/term-tree for sidebar

function children_sidebar_shortcode( $atts ) { ...READ MORE

answered Feb 26, 2022 in Others by narikkadan
• 63,420 points
627 views
0 votes
1 answer

What is the max length of page titles for SEO?

google displays only 50-60 words of a ...READ MORE

answered Feb 14, 2022 in Others by narikkadan
• 63,420 points
254 views
0 votes
1 answer

Why is just an ID in the URL path a bad idea for SEO?

yes it affects the click through rates ...READ MORE

answered Feb 20, 2022 in Others by narikkadan
• 63,420 points
313 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP