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 kitsguru - April 17, 2019

Ok so I am doing a custom plugin for an events table using the sample code. However I am getting an error.

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 === 'events') {
        $date = date("Y-m-d", @$_REQUEST['start_date']);
        $_REQUEST['permalink'] = $prefix . $date .'/'. @$_REQUEST['title'];
        trigger_error($_REQUEST['permalink']);
    }
}

The title is "Elsie's Show", prefix is "events"

The output I am getting is:

event/1969-12-31/Elsie's Show

and the error is 

Notice: event/1969-12-31/Elsie's Show in /Volumes/J/yaaws4/html/www/cmsb/plugins/yaaApp/yaaApp.php on line 125
Permalink can only contain these chars (a-z0-9/-.)!

It appears that the permalink is not getting processed to replace the spaces with hyphens and apostrophe stripped out etc.

Jeff Shields