-
-
Save markjaquith/c3ad2dcd44615653f768 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment