• Resolved V.E.L

    (@vezraimanuel)


    Hello 🙂

    There is problem with duplicate slug and native slug on regenerate/reset, this is the screenshot:

    View post on imgur.com

    The problem:
    1) I selected “Regenerate native slug”, but instead, i got new custom URLs and native slugs remains the same.

    2) The new generated URL doesn’t check for duplicates. so the link are all the same. i think the WordPress native slug always check for duplicates and adds number behind the URL if found. “wp_unique_post_slug” function i believe?

    many thanks! 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Maciej Bis

    (@mbis)

    Hi again @vezraimanuel,

    as far as I can see the native slugs are regenerated correctly (in the last column they are unique, as they have numeric indexes appended). The custom permalinks are not regenerated in “Regenerate native slug” mode, they are only displayed in the table (I will fix it in the next version of plugin, so “Old URI” and “New URI” columns will not be displayed then).

    The new URLs does not check for duplicates in your case. Normally a native slug would be used in the custom permalink, but after you used my custom snippet to trim the number of words – the rest of slug is removed (along with the numeric index appended to the end of slug).

    There is a hidden function that appends the numeric indexes also to the custom permalinks (in “Regenerate custom permalinks” mode). It might be useful in your case, because you use filtered slugs in your custom permalinks. You can enable it using a single line of code:

    add_filter('permalink_manager_fix_uri_duplicates', '__return_true');

    Best regards,
    Maciej

    Thread Starter V.E.L

    (@vezraimanuel)

    Hi @mbis

    many thanks!, i added the filter now… about the native slug, in the screenshot if you see the old one and new one are the same (very long, words not trimmed), I was expecting that regenerating/reset will also execute the custom snippet you gave me before (to trim the words). So the new native slug URI will have words trimmed out of it.

    thank you

    Plugin Author Maciej Bis

    (@mbis)

    Hi @vezraimanuel,

    ok, I see. Then, you need one more hook to filter the wp_unique_post_slug function.

    function pm_trim_native_slug($slug) {
    	$max_words = 5;
    	$words = explode('-', $slug);
    
    	if(count($words) > $max_words) {
    		$slug = implode("-", array_slice($words, 0, $max_words));
    	}
    	
    	return $slug;
    }
    add_filter('pre_wp_unique_post_slug', 'pm_trim_native_slug');

    Best regards,
    Maciej

    Thread Starter V.E.L

    (@vezraimanuel)

    Hi @mbis

    everything works, no more duplicates now, and I have all your functions and filter placed, tested with single post, works, but “pm_trim_native_slug” doesn’t affect regenerate/reset. result still giving me original slug made by wordpress (very long one), should i call the function from another hook?

    This is the screenshot:
    https://i.imgur.com/OTqlbiP.png

    This is the code in my functions.php:

    add_filter( 'permalink_manager_fix_uri_duplicates', '__return_true' );
    function pm_trim_native_slug($slug) {
    	$max_words = 10;
    	$words = explode('-', $slug);
    	if(count($words) > $max_words) {
    		$slug = implode("-", array_slice($words, 0, $max_words));
    	}
    	return $slug;
    }
    add_filter('pre_wp_unique_post_slug', 'pm_trim_native_slug');
    function pm_trim_custom_url($uri) {
    	$max_words = 10;
    	$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;
    		};
    	};
    	$new_title = trim($new_title, "/");
    	return $new_title;
    };
    add_filter( 'permalink_manager_filter_default_post_uri', 'pm_trim_custom_url', 99 );
    add_filter( 'permalink_manager_filter_default_term_uri', 'pm_trim_custom_url', 99 );

    thank you

    • This reply was modified 4 years, 11 months ago by V.E.L.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Problem with duplicate slug and native slug on Reset/Regenerate’ is closed to new replies.