function pof_taxonomy_icons_form($taxonomy_base_key, $items, $title, $title2)
{
    if (!current_user_can('manage_options')) {
        wp_die(__('You do not have sufficient permissions to access this page.'));
    }
    global $wpdb;
    $table_name = pof_taxonomy_icons_get_table_name();
    $agegroups = pof_taxonomy_icons_get_agegroups();
    if (isset($_POST['Submit'])) {
        // These files need to be included as dependencies when on the front end.
        require_once ABSPATH . 'wp-admin/includes/image.php';
        require_once ABSPATH . 'wp-admin/includes/file.php';
        require_once ABSPATH . 'wp-admin/includes/media.php';
        foreach ($_FILES as $key => $file) {
            $tmp = pof_taxonomy_icons_parser_taxonomy_key($key);
            $taxonomy_key = trim($tmp["key"]);
            $taxonomy_full_key = $taxonomy_base_key . "::" . $taxonomy_key;
            $agegroup_id = $tmp["agegroup_id"];
            if (array_key_exists("delete_" . $key, $_POST) && $_POST["delete_" . $key] == "delete") {
                $icon = pof_taxonomy_icons_get_icon($taxonomy_base_key, $taxonomy_key, $agegroup_id);
                if (!empty($icon)) {
                    wp_delete_attachment($icon[0]->attachment_id, false);
                }
                $wpdb->delete($table_name, array('taxonomy_slug' => $taxonomy_full_key, 'agegroup_id' => (int) $tmp["agegroup_id"]), array('%s', '%d'));
                echo "<br />Deleted " . $key . "";
            } else {
                if (!empty($file['name'])) {
                    $attachment_id = media_handle_upload($key, 0);
                    if (is_wp_error($attachment_id)) {
                        // There was an error uploading the image.
                        echo '<h1>ERROR ' . $key . '</h1>';
                        /*				echo '<pre>';
                        					print_r($_POST);
                        					print_r($_FILES);
                        					echo '</pre>';*/
                    } else {
                        $icon = pof_taxonomy_icons_get_icon($taxonomy_base_key, $taxonomy_key, $agegroup_id);
                        if (empty($icon)) {
                            $tmp = $wpdb->insert($table_name, array('taxonomy_slug' => $taxonomy_full_key, 'agegroup_id' => (int) $agegroup_id, 'attachment_id' => $attachment_id), array('%s', '%d', '%d'));
                            echo "<br />Added " . $key . "";
                        } else {
                            if (empty($icon)) {
                                wp_delete_attachment($icon[0]->attachment_id, false);
                            }
                            $tmp = $wpdb->update($table_name, array('attachment_id' => $attachment_id), array('taxonomy_slug' => $taxonomy_full_key, 'agegroup_id' => (int) $agegroup_id), array('%d'), array('%s', '%d'));
                            echo "<br />Updated" . $key . "";
                        }
                    }
                }
            }
        }
    }
    echo '<div class="wrap">';
    echo '<h1>' . $title . '</h1>';
    echo '<form id="featured_upload" method="post" action="" enctype="multipart/form-data">';
    echo '<table cellpadding="2" cellspacing="2" border="2">';
    echo '<thead>';
    echo '<tr>';
    echo '<th><h2>' . $title2 . '</h2></th>';
    foreach ($agegroups as $agegroup) {
        echo '<th><h2>' . $agegroup->title . '</h2></th>';
    }
    echo '</tr>';
    echo '</thead>';
    echo '<tbody>';
    foreach ($items as $tmp_key => $tmp_title) {
        $tmp_key = trim($tmp_key);
        echo '<tr>';
        echo '<th>' . $tmp_title . '<br /> (' . $tmp_key . ')</th>';
        foreach ($agegroups as $agegroup) {
            echo '<td>';
            $icon = pof_taxonomy_icons_get_icon($taxonomy_base_key, $tmp_key, $agegroup->id);
            if (empty($icon)) {
                echo "ei kuvaa<br />";
            } else {
                echo wp_get_attachment_image($icon[0]->attachment_id);
            }
            echo '<input type="file" name="taxonomy_icon_' . $tmp_key . '_' . $agegroup->id . '" id="taxonomy_icon_' . $tmp_key . '_' . $agegroup->id . '"  multiple="false" />';
            echo '<br /><input type="checkbox" name="delete_taxonomy_icon_' . $tmp_key . '_' . $agegroup->id . '" value="delete" /> Delete';
            echo '</td>';
        }
        echo '</tr>';
    }
    echo '</tbody>';
    echo '</table>';
    echo '<br /><input type="submit" name="Submit" value="Submit" />';
    echo '</form>';
    echo '</div>';
}
<?php

/*
Template Name: JSON Tag icons
*/
header('Content-type: application/json');
$ret = new stdClass();
$agegroups = pof_taxonomy_icons_get_agegroups();
function pof_pages_get_tag_icons($agegroups, $items, $item_tax_key)
{
    $items_arr = array();
    foreach ($agegroups as $agegroup_key => $agegroup) {
        $tmp = new stdClass();
        $tmp->agegroup = $agegroup->title;
        $tmp->post_guid = $agegroup->guid;
        $tmp->items = array();
        $agegroup_id = $agegroup->id;
        foreach ($items as $item_key => $item) {
            $tmp_item = new stdClass();
            $tmp_item->key = $item_key;
            $icon = pof_taxonomy_icons_get_icon($item_tax_key, $item_key, $agegroup_id, false);
            if (!empty($icon)) {
                $icon_src = wp_get_attachment_image_src($icon[0]->attachment_id);
                if (!empty($icon_src)) {
                    $tmp_item->icon = $icon_src[0];
                }
            }
            array_push($tmp->items, $tmp_item);
        }
        array_push($items_arr, $tmp);
    }