Skip to content

Instantly share code, notes, and snippets.

@tkjune
Last active December 11, 2015 22:59
Show Gist options
  • Save tkjune/4673900 to your computer and use it in GitHub Desktop.
Save tkjune/4673900 to your computer and use it in GitHub Desktop.
Extend WordPress plugin ADVANCED CUSTOM FIELDS api.
/**
* Display WordPress plugin ADVANCED CUSTOM FIELDS repeater field in table form. Based on field type, the layout varies.
* text, textarea: as plain text
* WYSIWYG: as HTML
* checkbox: as UL, display all choices and the chosen items are marked with "selected" class.
* select: as plain text for single choice. as UL for multiple choice, but only display chosen items
* gallery: as UL
* image: as IMG (for url or object) or as INT (for image_id).
* When return an image object, the title, alt, caption can be displayed.
* @required: Plugin: Advanced Custom Fields 3.5.8.1 above
* @param string $repeater_field_name Required. The repeater field which is the parent of all sub field.
* @param integer $post_id Optional. Specific post ID where your value was entered. Defaults to current post ID.
* @return void
* @author Joe
**/
function tkj_get_repeater_table( $repeater_field_name, $post_id = 0 )
{
$field = get_field_object( $repeater_field_name, $post_id );
$table_string = '';
if ( $field['type'] == 'repeater' && has_sub_field( $field['name'] ) )
{
$table_string .= "<table class=\"" . $field['name'] . "\"><caption>" . $field['label'] . "</caption><thead><tr>";
$field_columns = tkj_get_child_field_columns( $field['name'] );
foreach ( $field_columns as $field_column )
{
$table_string .= "<th>" . $field_column['label'] . "</th>";
}
$table_string .= "</tr></thead><tbody>";
foreach ( $field['value'] as $row )
{
$table_string .= "<tr>";
foreach ( $field_columns as $field_column )
{
$table_string .= "<td class=\"" . $field_column['type'] . "\">";
switch ( $field_column['type'] )
{
case 'gallery':
$list = "";
foreach ( $row[$field_column['name']] as $item )
{
$list .= "<li><img src=\"" . $item['url'] . "\" title=\"" . $item['title'] . "\" alt=\"" . $item['alt'] . "\">";
if ( $item['caption'] != '' )
{
$list .= "<br><span class=\"caption\">" . $item['caption'] . "</span>";
}
$list .= "<li>";
}
$table_string .= "<ul class=\"". $field_column['name'] . "\">";
$table_string .= $list;
$table_string .= "</ul>";
break;
case 'checkbox':
// var_dump($field_column['choices']);
$list = "";
foreach ( $field_column['choices'] as $choicekey => $choicevalue )
{
$list .= "<li";
foreach ( $row[$field_column['name']] as $item )
{
if ( $item == $choicevalue ) {
$list .= " class=\"selected\"";
}
// $table_string .= "<li>". $item . "</li>";
}
$list .= ">" . $choicevalue . "</li>";
}
$table_string .= "<ul class=\"". $field_column['name'] . "\">";
$table_string .= $list;
$table_string .= "</ul>";
break;
case 'image':
if ( is_array( $row[$field_column['name']] ) )
{
// var_dump($row[$field_column['name']]);
$table_string .= "<img src=\"" . $row[$field_column['name']]['url'] . "\" title=\"" . $row[$field_column['name']]['title'] . "\" alt=\"" . $row[$field_column['name']]['alt'] . "\">";
if ( $row[$field_column['name']]['caption'] != '' )
{
$table_string .= "<br><span class=\"caption\">" . $row[$field_column['name']]['caption'] . "</span>";
}
}
elseif ( is_numeric( $row[$field_column['name']] ) )
{
$table_string .= $row[$field_column['name']];
}
else
{
$table_string .= "<img src=\"" . $row[$field_column['name']] . "\">";
}
break;
case 'select':
if ( is_array( $row[$field_column['name']] ) )
{
// var_dump($row[$field_column['name']]);
$list = "";
foreach ( $row[$field_column['name']] as $item )
{
$list .= "<li>" . $item . "</li>";
}
$table_string .= "<ul class=\"". $field_column['name'] . "\">";
$table_string .= $list;
$table_string .= "</ul>";
}
else
{
$table_string .= $row[$field_column['name']];
}
break;
case 'text':
case 'textarea':
case 'radio':
$table_string .= $row[$field_column['name']];
break;
default:
$table_string .= $field_column['type'] . "<br>";
$table_string .= $row[$field_column['name']];
break;
}
$table_string .= "</td>";
}
$table_string .= "</tr>";
}
$table_string .= "</tbody></table>";
return array( 'order_no' => $field['order_no'], 'value' => $table_string );
}
else
{
return false;
}
}
function tkj_get_child_field_columns( $parent_field, $options=array('load_value' => false) )
{
$parent_field = get_field_object( $parent_field,0,$options );
if ( $parent_field )
{
$child_fields = $parent_field['sub_fields'];
return $child_fields;
}
else
{
return false;
}
}
# Display tables in order defined in backend
$fields = get_fields();
if( $fields )
{
$table_fields = array();
$i = 0;
foreach( $fields as $field_name => $value )
{
if ( $repeater_table = tkj_get_repeater_table( $field_name ) )
{
$table_fields[$i] = $repeater_table;
$order_no[$i] = $table_fields[$i]['order_no'];
$i++;
}
}
array_multisort( $order_no, SORT_ASC, $table_fields );
foreach ( $table_fields as $table_field )
{
echo $table_field['value'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment