Changes are made at your own risk and are not guaranteed to work. Always make backups of the database and files prior to implementing hacks. Make a special backup of the file and/or template you are modifying so that you can roll back quickly.
Hacks are dangerous and can cause your site to stop working. They make later updates to ExpressionEngine more difficult; you should track your hacks for post-update re-implementation.
Most hacks are unnecessary, please review the Development Documentation for information on expanding ExpressionEngine via Modules, Extensions, and Plugins.
Hacks are never officially supported.
URL titles of entries should be unique for each weblog.
If an entry having non-unique URL title is submitted, ExpressionEngine attaches a number symbol to the end of URL title.
This behavior is quite sensible in most cases. The problem arises when original non-unique URL title ends with a number. For example, if original non-unique URL title was “article_2”, then after appending additional number it will be “article_21” or “article_22”, which might be confusing for both content publishers and users.
This article discusses how ExpressionEngine’s functionality may be changed so that it would be possible
1) so specify that there should be a separator (or no separator) between original non-unique URL title and a string which is added by ExpressionEngine to ensure uniqueness;
2) to specify that a string which is added by ExpressionEngine to ensure uniqueness of URL title should be non-numeric or numeric.
The functionality related to non-unique URL titles is contained in “submit_new_entry()” function contained in system/cp/cp.publish.php file. Unfortunately this function does not have a hook which would allow to write an extension to change that functionality. Currently functionality related to non-unique URL titles can be changed by hacking system/cp/cp.publish.php file.
Before editing cp.publish.php file make a backup copy!
In order to make possible the changes outlined above you should open cp.publish.php file in text editor and go to approximately tha line 3670. There you will find the code
/** ---------------------------------
/** Is URL title unique?
/** ---------------------------------*/
$unique = FALSE;
$i = 0;
while ($unique == FALSE)
{
$temp = ($i == 0) ? $url_title : $url_title.$i;
$i++;
$sql = "SELECT count(*) AS count FROM exp_weblog_titles WHERE url_title = '".$DB->escape_str($temp)."' AND weblog_id = '$weblog_id'";
if ($entry_id != '')
{
$sql .= " AND entry_id != '$entry_id'";
}
$query = $DB->query($sql);
if ($query->row['count'] == 0)
{
$unique = TRUE;
}
// Safety
if ($i >= 50)
{
$error[] = $LANG->line('url_title_not_unique');
break;
}
}
$url_title = $temp;
}
// Did they name the URL title "index"? That's a bad thing which we disallow
if ($url_title == 'index')
{
$error[] = $LANG->line('url_title_is_index');
}
/** -------------------------------------
/** Validate Page URI
/** -------------------------------------*/
Change that portion of code with the following code:
/** ---------------------------------
/** Is URL title unique?
/** ---------------------------------*/
$unique = FALSE;
$i = 0;
$postfix_safety_num = 50;
$form_postfix = create_function('$i, $postfix_separator = "", $postfix_show_all = FALSE, $postfix_numerical = TRUE', '$postfix_letters_array = array(1=>"a",
2=>"b",
3=>"c",
4=>"d",
5=>"e",
6=>"f",
7=>"g",
8=>"h",
9=>"i",
10=>"j",
11=>"k",
12=>"l",
13=>"m",
14=>"n",
15=>"o",
16=>"p",
17=>"q",
18=>"r",
19=>"s",
20=>"t",
21=>"u",
22=>"v",
23=>"w",
24=>"x",
25=>"y",
26=>"z"
);
$postfix_letters_array_length = count($postfix_letters_array);
//echo $postfix_letters_array_length." ";
if ($postfix_show_all === TRUE)
{
$postfix_iteration_limit = $i;
}
elseif ($postfix_show_all === FALSE)
{
$postfix_iteration_limit = 1;
}
for ($j=1; $j<=$postfix_iteration_limit; $j++)
{
$postfix = "";
$postfix_remainder = "";
$postfix_index = "";
$postfix_left_symbol = "";
$postfix_right_symbol = "";
if ($postfix_show_all === FALSE)
{
$postfix_number = $i;
}
elseif ($postfix_show_all === TRUE)
{
$postfix_number = $j;
}
//echo $postfix_number;
if ($postfix_numerical === TRUE)
{
$postfix = $postfix_number;
}
elseif ($postfix_numerical === FALSE)
{
if ($postfix_number >= 1 AND $postfix_number <= $postfix_letters_array_length)
{
$postfix_index = $postfix_number;
$postfix = $postfix_letters_array[$postfix_index];
}
if ($postfix_number > $postfix_letters_array_length)
{
while ($postfix_number > $postfix_letters_array_length)
{
$postfix_remainder = $postfix_number % $postfix_letters_array_length;
//echo "[postfix_remainder: ".$postfix_remainder."]";
if ($postfix_remainder === 0)
{
$postfix_index = $postfix_letters_array_length;
}
elseif ($postfix_remainder > 0)
{
$postfix_index = $postfix_remainder;
}
$postfix_right_symbol = $postfix_letters_array[$postfix_index];
//echo "[postfix_right_symbol: ".$postfix_right_symbol."]";
$postfix_number -= $postfix_index;
$postfix_number /= 26;
//echo "[postfix_number: ".$postfix_number."]";
if ($postfix_number <= $postfix_letters_array_length)
{
$postfix_index = $postfix_number;
$postfix_left_symbol = $postfix_letters_array[$postfix_index];
//echo "[postfix_left_symbol: ".$postfix_left_symbol."]";
$postfix = $postfix_left_symbol.$postfix_right_symbol.$postfix;
}
elseif ($postfix_number > $postfix_letters_array_length)
{
$postfix = $postfix_right_symbol;
}
}
}
}
return $postfix_separator.$postfix;
}
');
while ($unique == FALSE)
{
$temp = ($i == 0) ? $url_title : $url_title.$form_postfix($i, '_', FALSE, FALSE);
$i++;
$sql = "SELECT count(*) AS count FROM exp_weblog_titles WHERE url_title = '".$DB->escape_str($temp)."' AND weblog_id = '$weblog_id'";
if ($entry_id != '')
{
$sql .= " AND entry_id != '$entry_id'";
}
$query = $DB->query($sql);
if ($query->row['count'] == 0)
{
$unique = TRUE;
}
// Safety
if ($i >= $postfix_safety_num)
{
$error[] = $LANG->line('url_title_not_unique');
break;
}
}
$url_title = $temp;
}
// Did they name the URL title "index"? That's a bad thing which we disallow
if ($url_title == 'index')
{
$error[] = $LANG->line('url_title_is_index');
}
/** -------------------------------------
/** Validate Page URI
/** -------------------------------------*/
Now let’s customize our new code.
First, find the line
$postfix_safety_num = 50;
It specifies how many non-unique URL titles can be handled before throwing alert.
Since there hardly will be more that 50 non-unique URL titles, you can safely leave that line as it is.
Secondly, find the line
$temp = ($i == 0) ? $url_title : $url_title.$form_postfix($i, '_', FALSE, FALSE);
Now you might want to change second and fourth argument of “$form_postfix()” function.
The second angument specifies what symbol or symbols will be used as a separator. Default is underscore; change it to what you like, only have in mind that using symbols illegal in URLs is not recommended. If you do not need a separator, enter empty string as the second argument.
The fourth argument specifies, numerical or non_numerical string will be attached. Default (FALSE) is non-numerical. If you need that numerical strings will be attached, change fourth argument from “FALSE” to “TRUE”.
That is! Now you have control over ExpressionEngine’s behavior regarding non-unique URLs.
Category:URLs Category:HacksCategory:HowToCategory:Tricks
