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?

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 Jesus - October 4, 2014

Hi folks,

Still no luck wile trying to implement this one. Here's the code I'm using:

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

I tried both options:

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

and

$requiredPrefix .= "blog/" . htmlspecialchars(@$_REQUEST['categoria']) . "/";

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'] . "/";}