x
 
Create New Page
 View Previous Changes    ( Last updated by Laisvunas )

Pre 1.6.5 url title creation

In ExpressionEngine’s version 1.6.5 url_title creation was modified to allow dashes, underscores, and periods regardless of the separator preference, and to allow uppercase characters.

For some projects pre-1.6.5 method of automatic url_title creation suits better.

To revert to older method you will need to use a hack - to change two functions with their pre-1.6.5 versions.

First, open file system/core/core.regex.php, find the function create_url_title() which is approximately at the line 980:

function create_url_title($str$lowercase FALSE)
    
{
        
global $PREFS;
        
        if (
function_exists('mb_convert_encoding'))
        
{
            $str 
mb_convert_encoding($str'ISO-8859-1''auto');
        
}
        
elseif(function_exists('iconv') AND ($iconvstr = @iconv('''ISO-8859-1'$str)) !== FALSE)
        
{
            $str 
$iconvstr;
        
}
        
else
        
{
            $str 
utf8_decode($str);
        
}
        
        
if ($lowercase === TRUE)
        
{
            $str 
strtolower($str);    
        
}
        
        $str 
preg_replace_callback('/(.)/', array($this"convert_accented_characters"), $str);
        
        
$str strip_tags($str);
        
        
// Use dash or underscore as separator        
        
$replace = ($PREFS->ini('word_separator') == 'dash') ? '-' '_';
        
        
$trans = array(
                        
'&\#\d+?;'                => '',
                        
'&\S+?;'                => '',
                        
'\s+'                    => $replace,
                        
'[^a-z0-9\-\._]'        => '',
                        
$replace.'+'            => $replace,
                        
$replace.'$'            => $replace,
                        
'^'.$replace            => $replace,
                        
'\.+$'                    => ''
                      
);
                       
        foreach (
$trans as $key => $val)
        
{
            $str 
preg_replace("#".$key."#i"$val$str);
        

        
        $str 
trim(stripslashes($str));

        return 
$str;
    

Change it to:

/** -------------------------------------------------
    /**  Create URL Title
    /** -------------------------------------------------*/

    
function create_url_title($str)
    
{
        
global $PREFS;
        
        if (
function_exists('mb_convert_encoding'))
        
{
            $str 
mb_convert_encoding($str'ISO-8859-1''auto');
        
}
        
elseif(function_exists('iconv') AND ($iconvstr = @iconv('''ISO-8859-1'$str)) !== FALSE)
        
{
            $str 
$iconvstr;
        
}
        
else
        
{
            $str 
utf8_decode($str);
        
}
        
        $str 
preg_replace_callback('/(.)/', array($this"convert_accented_characters"), $str);
        
        
$str strip_tags(strtolower($str));
        
$str preg_replace('/\&#\d+\;/'""$str);
        
        
// Use dash as separator        

        
if ($PREFS->ini('word_separator') == 'dash')
        
{
            $trans 
= array(
                            
"_"                                    => '-',
                            
"\&\#\d+?\;"                        => '',
                            
"\&\S+?\;"                          => '',
                            
"['\"\?\.\!*\$\#@%;:,\_=\(\)\[\]]"  => '',
                            
"\s+"                               => '-',
                            
"\/"                                => '-',
                            
"[^a-z0-9-_]"                        => '',
                            
"-+"                                => '-',
                            
"\&"                                => '',
                            
"-$"                                => '',
                            
"^-"                                => ''
                           
);
        
}
        
else // Use underscore as separator
        
{
            $trans 
= array(
                            
"-"                                    => '_',
                            
"\&\#\d+?\;"                        => '',
                            
"\&\S+?\;"                          => '',
                            
"['\"\?\.\!*\$\#@%;:,\-=\(\)\[\]]"  => '',
                            
"\s+"                               => '_',
                            
"\/"                                => '_',
                            
"[^a-z0-9-_]"                        => '',
                            
"_+"                                => '_',
                            
"\&"                                => '',
                            
"_$"                                => '',
                            
"^_"                                => ''
                           
);
        
}
                       
        
foreach ($trans as $key => $val)
        
{
            $str 
preg_replace("#".$key."#"$val$str);
        

        
        $str 
trim(stripslashes($str));

        return 
$str;
    

Then open the file system/cp/cp.publish.php, find the function liveUrlTitle() which is approximately at the line line 834:

/** ------------------------------------
        /**  Live URL Title Function
        /** -------------------------------------*/

        
function liveUrlTitle()
        
{
            
var defaultTitle '{$default_entry_title}';
            var 
NewText document.getElementById("title").value;
            
            if (
defaultTitle != '')
            
{
                
if (NewText.substr(0defaultTitle.length) == defaultTitle)
                
{
                    NewText 
NewText.substr(defaultTitle.length)
                
}    
            }
            
            NewText 
NewText.toLowerCase();
            var 
separator "{$word_separator}";
                
            
// Foreign Character Attempt
            
            
var NewTextTemp '';
            for(var 
pos=0pos<NewText.lengthpos++)
            
{
                
var NewText.charCodeAt(pos);
                
                if (
>= 32 && 128)
                
{
                    NewTextTemp 
+= NewText.charAt(pos);
                
}
                
else
                
{
                    {$foreign_replace}
                }
            }

            
            
var multiReg = new RegExp(separator '{2,}''g');
            
            
NewText NewTextTemp;
            
            
NewText NewText.replace('/<(.*?)>/g''');
            
NewText NewText.replace(/\s+/gseparator);
            
NewText NewText.replace(/\//g, separator);
            
NewText NewText.replace(/[^a-z0-9\-\._]/g,'');
            
NewText NewText.replace(/\+/gseparator);
            
NewText NewText.replace(multiRegseparator);
            
NewText NewText.replace(/-$/g,'');
            
NewText NewText.replace(/_$/g,'');
            
NewText NewText.replace(/^_/g,'');
            
NewText NewText.replace(/^-/g,'');
            
NewText NewText.replace(/\.+$/g,'');
            
            if (
document.getElementById("url_title"))
            
{
                document
.getElementById("url_title").value "{$url_title_prefix}NewText;            
            
}
            
else
            
{
                document
.forms['entryform'].elements['url_title'].value "{$url_title_prefix}NewText
            
}        
        } 

Change it to:

/** ------------------------------------
        /**  Live URL Title Function
        /** -------------------------------------*/

        
function liveUrlTitle()
        
{
            
var defaultTitle '{$default_entry_title}';
            var 
NewText document.getElementById("title").value;
            
            if (
defaultTitle != '')
            
{
                
if (NewText.substr(0defaultTitle.length) == defaultTitle)
                
{
                    NewText 
NewText.substr(defaultTitle.length)
                
}    
            }
            
            NewText 
NewText.toLowerCase();
            var 
separator "{$word_separator}";
            
            if (
separator != "_")
            
{
                NewText 
NewText.replace(/\_/gseparator);
            
}
            
else
            
{
                NewText 
NewText.replace(/\-/gseparator);
            
}
    
            
// Foreign Character Attempt
            
            
var NewTextTemp '';
            for(var 
pos=0pos<NewText.lengthpos++)
            
{
                
var NewText.charCodeAt(pos);
                
                if (
>= 32 && 128)
                
{
                    NewTextTemp 
+= NewText.charAt(pos);
                
}
                
else
                
{
                    {$foreign_replace}
                }
            }

    
            NewText 
NewTextTemp;
            
            
NewText NewText.replace('/<(.*?)>/g''');
            
NewText NewText.replace('/\&#\d+\;/g''');
            
NewText NewText.replace('/\&\#\d+?\;/g''');
            
NewText NewText.replace('/\&\S+?\;/g','');
            
NewText NewText.replace(/['\"\?\.\!*\$\#@%;:,=\(\)\[\]]/g,'');
            NewText = NewText.replace(/\s+/g, separator);
            NewText = NewText.replace(/\//g, separator);
            NewText = NewText.replace(/[^a-z0-9-_]/g,'');
            NewText = NewText.replace(/\+/g, separator);
            NewText = NewText.replace(/[-_]+/g, separator);
            NewText = NewText.replace(/\&/g,'');
            NewText = NewText.replace(/-$/g,'');
            NewText = NewText.replace(/_$/g,'');
            NewText = NewText.replace(/^_/g,'');
            NewText = NewText.replace(/^-/g,'');
            
            if (document.getElementById("url_title"))
            {
                document.getElementById("url_title").value = "{$url_title_prefix}" + NewText;            
            }
            else
            {
                document.forms['
entryform'].elements['url_title'].value = "{$url_title_prefix}" + NewText; 
            }        
        } 

That is! Now your url_titles will be created as in pre-1.6.5 versions of ExpressionEngine.

UPDATE: This hack is no longer needed since its functionality is available now via URL Title Symbols extension.

Category:URLs Category:URL titles Category:HacksCategory:HowToCategory:Tricks

Category:EE1