So with that hook would it instead not just be easier on the system to append the entry_id of the entry you are about to publish to the end of the url_title instead?
In my eyes, this would be the safest way, appending timestamps gives you even uglier URLs anyway and there can’t be two entry_ids with the same value (in one site) i suppose.
Mod Edit: Split from this feature request.
Will take a look at what you have said there - the print_r I mean. I see that a lot but never totally understand how to use it all and then even when I do see the results from those kinds of things I still don’t know how to go about modifying them on the fly.
Will take a look though. Really don’t think I will get it though.
Best wishes,
Mark
Ok here it goes: if you use the mentioned hook, you can do a simple print_r (see PHP docs, prints out the content of a variable) to see what and how it is passed through the hook.
Array
(
[weblog_id] => 1
[entry_id] =>
[entry_date] => 2008-06-05 11:08 AM
[expiration_date] =>
[comment_expiration_date] =>
[author_id] => 1
[new_weblog] => 1
[status] => open
[allow_comments] => y
[dst_enabled] => y
[trackback_urls] =>
[title] => A test entry
[url_title] => a_test_entry
[submit] => Submit
[mode] => normal
[field_id_1] =>
[field_ft_1] => xhtml
[field_id_2] =>
[field_ft_2] => xhtml
[field_id_3] =>
[field_ft_3] => xhtml
)
This $_POST array contains an entry
[url_title] => a_test_entry
which means you could start doing changes (e.g. appending the entry_id) to it. To append the entry id, you’d have to use a later hook as here, the entry_id coming from the post is, of course, still empty.
Just giving you some ideas.
Hiya,
Thanks for that. Still not entirely certain what you did there but I will take a look at this myself and see if I can get my head around it. May take a while though as I am still stuck with a plugin that I am trying to creates (no-one seems to like me in the plugins forum :down: ).
Thanks again for the pointers on this though.
Best wishes,
Mark
My tip would be that I need to get more sleep (hmm actually that’s more a tip for me really! 😉 ).
Unfortunately other work got in the way of me looking at this one so had to take a back-burner. I may when I get the chance take another look into doing this as I do feel that it is something that is really really needed personally.
Sorry that I don’t have any information other than I slacked out on this one 😊
Best wishes,
Mark
So I finally tried to get my head around extensions and took a run at this.
It seems to work, but since this is my first run, I’d be very grateful for any tips or improvements anyone can offer:
<?php
class entry_id_in_url_title
{
var $settings = array();
var $name = 'Entry_id in url_title';
var $version = '1.0';
var $description = 'Inserts entry_id into url_title when submitting new entry';
var $settings_exist = 'n';
var $docs_url = '';
function entry_id_in_url_title($settings='')
{
$this->settings = $settings;
}
function insert_entry_id($entry_id, $data, $ping_message)
{
if (array_key_exists('entry_id', $data)) // only perform if this is a new entry (not an update)
{
$new_url_title = $entry_id.'-'.$data['url_title'];
$_POST['url_title'] = $new_url_title; // in case any other extensions are making use of the $_POST data
// save to the database
global $DB, $FNS;
$update_data = array('url_title' => $new_url_title);
$sql = $DB->update_string('exp_weblog_titles', $update_data, "entry_id = '{$entry_id}'");
$DB->query($sql);
$FNS->clear_caching('all');
}
}
function activate_extension()
{
global $DB;
$DB->query($DB->insert_string('exp_extensions',
array(
'extension_id' => '',
'class' => 'entry_id_in_url_title',
'method' => 'insert_entry_id',
'hook' => 'submit_new_entry_end',
'settings' => '',
'priority' => 10,
'version' => $this->version,
'enabled' => 'y'
)
)
);
}
function disable_extension()
{
global $DB;
$DB->query("DELETE FROM exp_extensions WHERE class = 'entry_id_in_url_title'");
}
}
?>
I’ve edited the code above to include the clear_caching function at the end. It appears that submit_new_entry_end is called after EE clears the caches upon submitting a new entry. If you use url_titles to link to entries, you risk 404s without this line, since the url_title will have been changed after the caches have been refreshed and pages rebuilt.
I’ve added some logic to RobJ25’s original extension that adds the following:
Here’s the files you need to make it work. Make sure to customize the settings once you’ve activated the extension:
ext.entry_id_in_url_title.php
<?php
class Entry_id_in_url_title
{
var $settings = array();
var $name = 'Entry_id in url_title';
var $version = '1.1';
var $description = 'Inserts entry_id into url_title when submitting new entry to maintain unique URL titles';
var $settings_exist = 'y';
var $docs_url = '';
function Entry_id_in_url_title($settings='')
{
$this->__construct($settings);
}
function __construct($settings='')
{
$this->settings = $settings;
}
function insert_entry_id($entry_id, $data, $ping_message)
{
global $PREFS, $DB, $FNS;
if (array_key_exists('entry_id', $data) && array_search($data['weblog_id'], $this->settings['chosen_weblogs']) !== FALSE) // only perform if this is a new entry (not an update) and a managed weblog
{
$sep = ($PREFS->ini('word_separator') == 'dash' ? '-' : '_');
if ($this->settings['position_of_id'] == 'front')
{
$new_url_title = $entry_id.$sep.$data['url_title'];
}
else
{
$new_url_title = $data['url_title'].$sep.$entry_id;
}
$_POST['url_title'] = $new_url_title; // in case any other extensions are making use of the $_POST data
// save to the database
$update_data = array('url_title' => $new_url_title);
$sql = $DB->update_string('exp_weblog_titles', $update_data, "entry_id = '".$entry_id."'");
$DB->query($sql);
$FNS->clear_caching('all');
}
}
function settings()
{
global $DB;
$query = $DB->query("SELECT weblog_id, blog_title FROM exp_weblogs ORDER BY blog_title ASC");
$weblogs = array();
if ($query->num_rows > 0)
{
foreach($query->result as $row)
{
$weblogs[$row['weblog_id']] = $row['blog_title'];
}
}
$settings = array();
$settings['chosen_weblogs'] = array(
'ms',
$weblogs,
''
);
$settings['position_of_id'] = array(
'r',
array(
'front' => 'prepend',
'back' => 'append'
),
'back'
);
return $settings;
}
function activate_extension()
{
global $DB;
$DB->query($DB->insert_string('exp_extensions',
array(
'extension_id' => '',
'class' => 'entry_id_in_url_title',
'method' => 'insert_entry_id',
'hook' => 'submit_new_entry_end',
'settings' => '',
'priority' => 10,
'version' => $this->version,
'enabled' => 'y'
)
)
);
}
function disable_extension()
{
global $DB;
$DB->query("DELETE FROM exp_extensions WHERE class = 'entry_id_in_url_title'");
}
}
lang.entry_id_in_url_title.php
<?php
$L = array(
//----------------------------------------
// Required for settings
//----------------------------------------
'chosen_weblogs' => 'Choose weblogs to have entry_ids added to url titles',
'position_of_id' => 'Choose location of entry_id',
'prepend' => 'Prepend to url_title (ex: "123-my-entry-title")',
'append' => 'Append to url_title (ex: "my-entry-title-123")',
// END
''=>''
);
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.