using field:label in permalink? hack or feature request?

By kitsguru - March 21, 2019

I have a content_type list field that I want to use to build the permalink with using the label.

e.g. content_type list

1|meals
2|activity
3|special event

I want the permalink to look like:

meals/lunch
meals/dinner
activity/art-show
activity/bingo
special-event/st-paddys-day
special-event/mothers-day-festival

I tried using the permalink autopopulate field but it gives:

1-lunch

Any ideas on how I might set this up or can we make it a feature request to be able to use dynamic prefixes based on record content?

Jeff Shields

By daniel - March 22, 2019

Hi Jeff,

It is possible to customize the permalink format using some custom plugin code included in the Permalinks readme.txt under "ADVANCED: CREATING CUSTOM PERMALINK FORMATS ON RECORD SAVE WITH A PLUGIN". For reference, here is the code: 

<?php
/*
Plugin Name: Permalinks Custom
Description: Code for customized permalinks can be defined in this plugin
Version: 1.00
Requires at least: 2.53
*/

addAction('record_presave', 'permalink_custom_permalinks', 1, 3);

//
function permalink_custom_permalinks($tableName, $isNewRecord, $oldRecord) {
  $schema = loadSchema($tableName);
  $prefix = @$schema['permalink']['defaultValue'];
  
  // create custom permalink on first save of record
  if ($tableName == 'your_table_name' && !$oldRecord) {
    $_REQUEST['permalink'] = $prefix . @$_REQUEST['title'] .'/'. @$_REQUEST['user_name'];
  }
}
?>

This code will need to be customized to suit your tables/fields, placed in a new file in the plugins directory, and activated through the Plugins menu. A few additional things to be aware of:

  • This will use the value submitted by the list, so you'll need to change the list definition to use the text you want in the URL rather than a number, e.g.:
    meals|meals
    activity|activity
    special-event|special event​
  • The above will only trigger on a newly saved record; if you want it to update any time the record is modified, you can remove "&& !$oldRecord"

I hope this helps get you started; let me know if you have any further questions!

Thanks,

Daniel
Technical Lead
interactivetools.com

By daniel - April 17, 2019

Hi Jeff,

You're right - the value you're generating is going through the same error checking as if it was manually entered into the permalink field. You should be able to correct this by adding the following to the end of your if() block (where you're currently calling trigger_error()):

$_REQUEST['permalink'] = _permalink_generate_formatInputText( $_REQUEST['permalink'] );

Also, it looks like start_date is either empty or doesn't contain a timestamp, causing it to default to 1969-12-31. One quick thing you can try to fix that is add a strtotime() call to the date generation:

$date = date("Y-m-d", strtotime(@$_REQUEST['start_date']));

(strtotime() is a really handy function that can convert most string representations of dates into a timestamp: https://www.php.net/manual/en/function.strtotime.php)

Let me know if this helps sort out your issue!

Thanks,

Daniel
Technical Lead
interactivetools.com