Combine name field and category :label on save

By Mikey - October 25, 2017

I'm trying to combine a records "Name" field and "Category" selection from a dropdown list and place the results into a field called new_name. Below is the plugin code I'm using, but it doesn't work with :label

Any suggestions on how to get the label for news_category:label to work?

<?php
/*
Plugin Name: Combine news record Name and Category.
Description: Combine the news record Name and Category and place in the new_name field.  
Version: 1.00
Requires at least: 2.00
*/

// UPDATE THESE VALUES

// DON'T UPDATE ANYTHING BELOW THIS LINE

addAction('record_postsave', 'combineNameCategoryNumber', 0, 4); 
//

function combineNameCategoryNumber($tableName, $isNewRecord, $oldRecord, $recordNumber) {
  if ($tableName!='news'){ return; }
  mysql_update($tableName, $recordNumber, null, array('new_name' => $_REQUEST['name'] . ' ' . $_REQUEST['news_category:label']));
  
}
?>

Thanks,

Zicky

By Dave - October 27, 2017

Hi Zicky, 

The first thing you want to do is add some debug code to see what variables you have to work with.  Try: 

showme($_REQUEST);
exit;

If new_category is a number you'll have to load the label manually.  Check out the function getListOptionsFromSchema().  You can pass it a fieldSchema to get the values for that field, eg: (untested code)

$fieldSchema    = $GLOBALS['schema']['news_category'];
$valuesToLabels = getListOptionsFromSchema(fieldSchema);
$fieldLabel     = @$valuesToLabels[ @$_REQUEST['news_category'] ];

But you'll want to use showme() on the different variables if it doesn't work at first try to see what data you're getting.

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com