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

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

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