Permalink categories

4 posts by 3 authors in: Forums > CMS Builder: Plugins & Add-ons
Last Post: June 18, 2015   (RSS)

By Maurice - June 12, 2015

Hi It Team,

My customer wants to select a category in her article. ( a dropdown listfield in her section)

can we use this category automated in te permalink?

example http://domain.com/category/article-permalink

that would be great if some one got a solution thnx

Greetz Maurice

-------------------------------------------

Dropmonkey.nl

By Maurice - June 17, 2015

Posted 6 days ago and still no answer??? or has it been posted before??? or did i miss somthing in the manual?

-------------------------------------------

Dropmonkey.nl

By Dave - June 18, 2015

Hi Maurice, 

I don't think Permalinks does exactly what you want.

I seem to recall seeing someone working on something like that in the forum.  I couldn't find it with a quick search, though, and I'm not sure how far they got.

You can specify multiple input fields, but it will join them with '-', so if you have this in the plugin: 

$GLOBALS['PERMALINKS']['autopopulate_fromFields'] = array('category title', 'name', 'title');

Then you'd get http://example.com/category-article-title/ and it would also use the "value" not "label" of the category, so that might be the category num.

Another thing you can do is specify a value under "Default Value" in the field editor such as "/articles/" to make that a required prefix for all the permalinks created in that section.

But it sounds like you want the text of the dynamically selected category.  It doesn't do that, but you could try writing a plugin to "override" the permalink that's being sent.  

Here's one to get you started: 

<?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'];
  
  // 
  if ($tableName == 'your_articles_table') {
    $_REQUEST['permalink'] = $prefix . @$_REQUEST['category'] .'/'. @$_REQUEST['title'];
  }

}

// eof


Note that overriding the permalinks field value will prevent the user from setting a custom permalink.  Another way to do it might be to write a plugin that used javascript to update the permalink field when the user selected/changed categories, but you'd need to think about what it should do when adding records vs editting, etc. 

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com