• Resolved 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, it works.

    But i still have problem with Regenerate/Reset, as follows:

    1) When selecting “Regenerate native slugs”, trim function doesn’t work on native slugs:
    https://i.imgur.com/Qpjqo3B.png

    2) When selecting “Use original URLs as custom permalinks”, trim function doesn’t work on native slugs, and New URI still have duplicates:
    https://i.imgur.com/bnbyvi6.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

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

    (@mbis)

    Hi @vezra

    please use this code instead.

    
    /**
     * Trim native slugs
     */
    function pm_trim_native_slug($slug, $post_ID, $post_status, $post_type, $post_parent) {
    	global $wpdb;
    
    	$max_words = 10;
    	$words = explode('-', $slug);
    
    	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);
    
    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 );
    
    /**
     * Make the custom URIS unique
     */
    function pm_make_uris_unique($default_uri, $original_slug, $element, $custom_slug, $native_uri) {
    	global $permalink_manager_uris;
    
    	// if($native_uri ) { return $default_uri; }
    
    	// Get the element ID
    	$element_id = (!empty($element->taxonomy)) ? "tax-{$element->term_taxonomy_id}" : $element->ID;
    
    	// Check if the same URI was used for another post or term
    	$all_uris = $permalink_manager_uris;
    
    	if(in_array($default_uri, $all_uris)) {
    		unset($all_uris[$element_id]);
    
    		$duplicates = array_count_values($all_uris);
    		$duplicates_count = (isset($duplicates[$default_uri])) ? $duplicates[$default_uri] : 0;
    
    		if($duplicates_count) {
    			$suffix = intval($duplicates_count) + 1;
    			$default_uri = preg_replace('/(.+?)(\.[^\.]+$|$)/', '$1-' . $suffix . '$2', $default_uri);
    		}
    	}
    
    	return $default_uri;
    }
    add_filter('permalink_manager_filter_default_post_uri', 'pm_make_uris_unique', 99, 5);
    add_filter('permalink_manager_filter_default_term_uri', 'pm_make_uris_unique', 99, 5);
    
    add_filter('permalink_manager_fix_uri_duplicates', '__return_true' );
    • This reply was modified 5 years, 10 months ago by Maciej Bis.
    Thread Starter V.E.L

    (@vezraimanuel)

    Hi @mbis

    this time works as expected, sometimes it still generates duplicate custom URI (not native slug) after “Save” in single post editor, but that’s fine since we can fix it easily from Tools>Permalink Manager screen.

    I tested regenerate/reset with all options available, perfect results, no more duplicates, and word trim works just fine.

    many thanks for this!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Still have problem with slug results on Reset/Regenerate’ is closed to new replies.