secret
Last active

  • Download Gist
clean-slugs.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
<?php
/*
Plugin Name: Clean Slugs
Description: Disallows foreign and encoded characters in post slugs
Version: 0.1
Author: Mark Jaquith
Author URI: http://coveredwebservices.com/
*/
 
class CWS_Clean_Post_Slugs {
static $instance;
function __construct() {
self::$instance =& $this;
add_filter( 'wp_insert_post_data', array( $this, 'update_slug' ) );
}
 
function update_slug( $data ) {
$data['post_name'] = $this->sanitize( $data['post_name'] );
return $data;
}
 
// Mostly copied from WordPress' sanitize_title_with_dashes()
function sanitize( $title ) {
$title = strip_tags($title);
// Preserve escaped octets.
$title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
// Remove percent signs that are not part of an octet.
$title = str_replace('%', '', $title);
// Remove octets.
$title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '', $title);
 
$title = remove_accents($title);
if (seems_utf8($title)) {
if (function_exists('mb_strtolower')) {
$title = mb_strtolower($title, 'UTF-8');
}
// $title = utf8_uri_encode($title, 200);
}
 
$title = strtolower($title);
$title = preg_replace('/&.+?;/', '', $title); // kill entities
$title = str_replace('.', '-', $title);
$title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
$title = preg_replace('/\s+/', '-', $title);
$title = preg_replace('|-+|', '-', $title);
$title = trim($title, '-');
 
return $title;
}
}
 
new CWS_Clean_Post_Slugs;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.