Skip to content

Instantly share code, notes, and snippets.

@WazzaJB
Created December 30, 2014 14:47
Show Gist options
  • Save WazzaJB/f7c75e05a325c86dd421 to your computer and use it in GitHub Desktop.
Save WazzaJB/f7c75e05a325c86dd421 to your computer and use it in GitHub Desktop.
Advanced Custom Fields and Shopp Category UI Fix
<?php
/*
Simply include this file from your functions.php within your WordPress theme, do everything else as you would!
*/
if( ! class_exists('acf_shopp_category') ) :
class acf_shopp_category {
function __construct()
{
// actions
add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));
add_action('shopp_category_saved', array($this, 'save_category'), 10, 1);
}
function admin_enqueue_scripts()
{
// Ensure this is a shopp category screen
if( $_GET['page'] != 'shopp-categories' || !isset($_GET['id']) )
{
return;
}
// load acf scripts
acf_enqueue_scripts();
// Start field group metabox creation by setting up vars
$term = $_GET['id'];
$taxonomy = 'shopp_category';
$post_id = "{$taxonomy}_{$term}";
$args = array('taxonomy' => $taxonomy);
// Get all the field groups for the taxonomy
$field_groups = acf_get_field_groups( array('taxonomy' => $taxonomy) );
// Providing we have field groups
if( empty($field_groups) )
{
return false;
}
// Setup the form data and nonce
add_action('submitpage_box', array($this, 'add_form_data'));
// Loop over the groups
foreach( $field_groups as $field_group )
{
// Get the groups fields
$fields = acf_get_fields( $field_group );
// Setup the metabox for group, passing the fields
add_meta_box(
'shopp-acf-category-fields',
$field_group['title'],
array($this, 'field_group_metabox'),
'shopp_page_shopp-category',
'advanced',
'default',
array('post_id' => $post_id, 'fields' => $fields)
);
}
}
function add_form_data()
{
$post_id = "shopp_category_{$_GET['id']}";
acf_form_data(array(
'post_id' => $post_id,
'nonce' => 'shopp-category'
));
}
function field_group_metabox( $category, $metabox )
{
// Render the fields within the metabox
?>
<table class="form-table">
<tbody>
<?php acf_render_fields( $metabox['args']['post_id'], $metabox['args']['fields'], 'div', 'field' ); ?>
</tbody>
</table>
<?php
}
function save_category( $category )
{
// Verify the nonce
if( ! acf_verify_nonce('shopp-category') )
{
return $category->term_id;
}
// Save the category fields provided
if( acf_validate_save_post(true) )
{
acf_save_post("{$category->taxonomy}_{$category->term_id}");
}
}
}
new acf_shopp_category();
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment