Permalinks url question

By Jesus - August 29, 2014

Hi,

I'm wondering if its possible to have a different url (filling the permalink field) while selecting a category from a select box.

Example:

I've a select box with 3 categories: News, Info and Articles

and I want to have my permalink to be something like:

/blog/news/article-name if News category was selected

/blog/info/article-name if Info category was selected

/blog/articles/article-name if Articles category was selected

Thanks in advance!

By Daryl - August 29, 2014

Hi Jesus,

Yes, it's possible to add additional URL to the permalink depending on the selected category from a drop down list field.

One way is to customize the permalink plugin by adding the selected category as prefix.

Assuming that your category field is not in another section and with human-readable values, for example, news|News and info|Info and its fieldname is "category".
We can add the selected category value as prefix by appending the selected category value to $requiredPrefix variable inside the permalink_cms_onPreSaveAutopopulate() function:

function permalink_cms_onPreSaveAutopopulate($tableName, $isNewRecord, $oldRecord) {
  [...]
  // is autopopulate needed?
  $permalinkText  = @$_REQUEST['permalink'];
  $requiredPrefix = $GLOBALS['schema']['permalink']['defaultValue'];
  $requiredPrefix .= "blog/" . htmlspecialchars(@$_REQUEST['category']) . "/";
  $doAutopopulate = ($permalinkText == '' || $permalinkText == $requiredPrefix);
  if (!$doAutopopulate) { return; }
  [...]
}

I copied parts of the function's code for reference, the red texts are the line of code that we need to add. Please note that this will only work if the permalink is auto-populated so you can still add a custom permalink URL manually.

And please let me know any further questions and don't forget to back up your files and database in case you want to revert them back prior to these changes.

Thanks,

Daryl Maximo
PHP Programmer - interactivetools.com

By Jesus - August 29, 2014

Great Daryl!

One more thing... what if I'm also using the permalinks on another sections?

Will try to explain it better... this example was for the blog and I understood perfectly what it needs to be done; but lets say I also have a couple more sections in my CMSBuilder admin (products and services). On this 2 sections, I don't use the categories (or even a category field) on it.

Will you please clarify this a little for me? (if possible :) )

Thanks!

By Daryl - August 29, 2014

That's a good question.

If you want to implement this to a particular section, then we need to add an if block to check for the tablename. For example:

if ($tableName == "blog"){
    $requiredPrefix .= "blog/" . @$_REQUEST['category'] . "/";
}

Please let me know if that works for you.

Thanks,

Daryl Maximo
PHP Programmer - interactivetools.com

By Jesus - August 29, 2014

AWESOME! Will give this a try!

Thanks a lot!

By Jesus - September 16, 2014 - edited: September 16, 2014

Hi Daryl,

Here's the code I'm trying, with no luck yet. Am I missing something:

  if ($tableName == "cms_blog"){
     $requiredPrefix .= "blog/" . @$_REQUEST['categoria'] . "/";}

Should I keep or not the cms_ prefix on the table name?

By gregThomas - September 19, 2014

Hi Jesus, 

You don't need to include the table prefix, you can just use the section name instead:

if ($tableName == "blog"){
   $requiredPrefix .= "blog/" . @$_REQUEST['categoria'] . "/";}

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By gregThomas - October 6, 2014

Hi Jesus,

I've found a much easier way to do this. At the top of the plugin is a line where you can set what fields are used to create the permalink, if you add the item menu to it, the section name will be added to the permalink field:

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

By doing this you won't need to make any other changes to the plugin, the only downside is that dashes will be used to separate the permalink variables instead of the forward slashes. 

The menu variable is a hidden field that is used by the CMS to store the current section that is being edited. 

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By zaba - October 8, 2014

is this not a case of the old classic typo? categoria, should be category (based on your previous post)

     $requiredPrefix .= "blog/" . @$_REQUEST['categoria'] . "/";}