in reply to Re: WordPress 'sanitize_title'
in thread WordPress 'sanitize_title'

> So you found sanitize_title (you're good enough at PHP)

Well, I didn't find it by checking the source but during a google research. Someone pointed to the sanitize_title function when it comes to permalink creation.

So I downloaded the WordPress source code and found it in formatting.php.

To be honest I also found the apply_filters function in wp-includes/plugin.php when trying to understand what "sanitize_title" does.

But apply_filters as well as sanitize_title gives me no clue what WordPress does to convert the title. I expected to find some php string manipulation but apply_filters is all greek to me.

function apply_filters($tag, $value) { global $wp_filter, $merged_filters, $wp_current_filter; $args = array(); $wp_current_filter[] = $tag; // Do 'all' actions first if ( isset($wp_filter['all']) ) { $args = func_get_args(); _wp_call_all_hook($args); } if ( !isset($wp_filter[$tag]) ) { array_pop($wp_current_filter); return $value; } // Sort if ( !isset( $merged_filters[ $tag ] ) ) { ksort($wp_filter[$tag]); $merged_filters[ $tag ] = true; } reset( $wp_filter[ $tag ] ); if ( empty($args) ) $args = func_get_args(); do { foreach( (array) current($wp_filter[$tag]) as $the_ ) if ( !is_null($the_['function']) ){ $args[1] = $value; $value = call_user_func_array($the_['function'], array +_slice($args, 1, (int) $the_['accepted_args'])); } } while ( next($wp_filter[$tag]) !== false ); array_pop( $wp_current_filter ); return $value; }

My hope and the reason for my posting was, that someone already has written a module/function/... in Perl that accomplished this. It seems this is not the case.

Cheers Klammer

Replies are listed 'Best First'.
Re^3: WordPress 'sanitize_title'
by Anonymous Monk on Sep 01, 2008 at 16:38 UTC
    Once again you just give up too easily. PHP is no harder than Perl. You're just being lazy.
    sanitize_title applied to a post title by the sanitize_title function, after stri +pping out HTML tags. wordpress/wp-includes/taxonomy.php: $value = apply_filters("pre +_term_$field", $value, $taxonomy); wordpress/wp-includes/default-filters.php:$filters = array('pre_term_s +lug'); wordpress/wp-includes/plugin.php:function apply_filters($tag, $value) +{ wordpress/wp-includes/formatting.php:function sanitize_file_name( $nam +e ) { // Like sanitize_title, but with periods wordpress/wp-includes/formatting.php:function sanitize_title($title, $ +fallback_title = '') { wordpress/wp-includes/formatting.php: $title = apply_filters('sanit +ize_title', $title); wordpress/wp-includes/taxonomy.php: $args['query_var'] = saniti +ze_title_with_dashes($args['query_var']); wordpress/wp-includes/taxonomy.php: $args['rewrite']['slug' +] = sanitize_title_with_dashes($taxonomy); wordpress/wp-includes/taxonomy.php: $value = sanitize_title($va +lue); wordpress/wp-includes/taxonomy.php: $slug = sanitize_title($slu +g); wordpress/wp-includes/taxonomy.php: if ( '' === $slug = sanitize_ti +tle($term) ) wordpress/wp-includes/taxonomy.php: $slug = sanitize_title($nam +e); wordpress/wp-includes/taxonomy.php: $slug = sanitize_title($slu +g, $term_id); wordpress/wp-includes/taxonomy.php: $slug = sanitize_title($nam +e); wordpress/wp-includes/taxonomy.php: $slug = sanitize_title($nam +e, $term_id); function add_filter($tag, $function_to_add, $priority = 10, $accepted_ +args = 1) { global $wp_filter, $merged_filters; $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priorit +y); $wp_filter[$tag][$priority][$idx] = array('function' => $function_ +to_add, 'accepted_args' => $accepted_args); unset( $merged_filters[ $tag ] ); return true; } // Slugs $filters = array('pre_term_slug'); foreach ( $filters as $filter ) { add_filter($filter, 'sanitize_title'); } add_filter('sanitize_title', 'sanitize_title_with_dashes'); function sanitize_title_with_dashes($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); // Restore octets. $title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $ +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 = preg_replace('/[^%a-z0-9 _-]/', '', $title); $title = preg_replace('/\s+/', '-', $title); $title = preg_replace('|-+|', '-', $title); $title = trim($title, '-'); return $title; }
    http://www.w3schools.com/php/func_string_strip_tags.asp
    http://www.php.net/preg_replace
    http://www.php.net/strip_tags
    http://www.php.net/trim
    http://www.php.net/strtolower
    WordPress › Support » How does WP remove accents from Polish characters?
    http://codex.wordpress.org/Function_Reference_2.0.x

    http://adambrown.info/p/wp_hooks/hook/sanitize_title?version=2.6&file=wp-includes/formatting.php
    http://postedpost.com/2008/06/22/hack-wordpress-sanitize-title-function/
    http://postedpost.com/2008/06/23/ultimate-wordpress-post-name-url-sanitize-solution/
      > You're just being lazy.
      In defense I can only say, that I'm not a good perl scripter too. :)

      I'm really trying hard. I don't expect anybody to write this for me in case you fear I'm that sort of guy.

      As far as I understand your posted code, sanitize_title_with_dashes is the function to look for. Finally some string manipulation. :)

      Thanks for all the other helpful links. I'll have a look at it and will try to convert sanitize_title_with_dashes into perl code.

      Cheers Klammer