1

been struggling with a bug. On created_{taxonomy} hook yet in edited_{taxonomy} hook works and upload the expected file with no issues. As per the following code I added custom field type along with custom taxonomy type. works fine.

add_action( 'init', 'addingCustomType' ); 


function addingCustomType() { 
    register_post_type( 'work', 
        array( 
            'labels' => array( 
                'name' => 'Works', 
                'singular_name' => 'work', 
                'add_new' => 'Add New', 
                'add_new_item' => 'Add New Work', 
                'edit' => 'Edit', 
                'edit_item' => 'Edit Work', 
                'new_item' => 'New Work', 
                'view' => 'View', 
                'view_item' => 'View Work', 
                'search_items' => 'Search Works', 
                'not_found' => 'No Works found', 
                'not_found_in_trash' =>  
                    'No Works found in Trash', 
                'parent' => 'Parent Work'                     
            ), 
            'public' => true, 
            'show_in_rest' => true,
            'rest_base'    => 'works',
            'menu_position' => 20, 
            'supports' => array( 'title', 'editor', 'thumbnail' ), 
            'taxonomies' => array( 'work_type' ), 
            'menu_icon' =>  
                plugins_url( 'works.png', __FILE__ ), 
            'has_archive' => true,
            'exclude_from_search' => true 
        ) 
    );

    register_taxonomy( 
        'work_type',       
        'work',                  
        array( 
            'labels' => array( 
                'name' => 'Work Type', 
                'add_new_item' => 'Add New Work Type', 
                'new_item_name' => 'New Work Type Name'
            ), 
            'show_ui' => true, 
            'show_tagcloud' => false, 
            // 'hierarchical' => true,
            'public' => true, 
            // 'show_in_rest' => true,
            // 'rest_base'    => 'work_type',
        )
    ); 
}

As per the following code is to add enctype on both edit and add(new)to my custom taxonomy (work_type) works fine it is visible in both forms.

add_action('work_type_term_edit_form_tag', 'update_edit_form' );
add_action('work_type_term_new_form_tag', 'update_edit_form' );

function update_edit_form() {
    echo ' enctype="multipart/form-data"';
}

As per the following code is to add upload file input field to the admin panel on both scenario add & edit which works great.

add_action( 'work_type_edit_form_fields', 'ch4_br_book_type_new_fields', 10, 2 );
add_action( 'work_type_add_form_fields', 'ch4_br_book_type_new_fields', 10, 2 );


function ch4_br_book_type_new_fields( $tag ) {
    $mode = is_object( $tag ) ? 'edit' : 'new';
   

    switch($mode) {
        case 'edit': 
            // icon
            echo '<div class="form-field">';
            echo '<label for="tag-category-url">Choose an icon</label>';
            echo '<input type="file" id="typeIcon" name="typeIcon">';
            echo '</div>';  
      

        break;
        case 'new':
            // icon
            echo '<div class="form-field">';
            echo '<label for="tag-category-url">Choose an icon</label>';
            echo '<input type="file" id="typeIcon" name="typeIcon">';
            echo '</div>';  
        break;
    }
}

As per the following code I'm trying to use wp upload funtion to upload the file which works great in edited_{taxonomy} hook. However it is not working in created/create_{taxonomy} hook & $_FILES being empty with file['error'] = "Specified file failed upload test."

add_action( 'edited_work_type', 'ch4_br_save_book_type_new_fields', 10, 2 );
add_action( 'created_work_type', 'ch4_br_save_book_type_new_fields', 10, 2 );
function ch4_br_save_book_type_new_fields( $term_id, $tt_id ) {
   

    if ( !$term_id ) {
        return;
    }

  
    if ( ! function_exists( 'wp_handle_upload' ) ) {
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
    }

    $uploadedfile = $_FILES["typeIcon"];
    $upload_overrides = array( 'test_form' => false );
    $iconFile  = wp_handle_upload( $uploadedfile, $upload_overrides );
    if ($iconFile['error']) { 
        print_r($iconFile['error']);
        // print_r("file", $_FILES);
    }

    $returnvalue =  update_term_meta( $term_id, 'work_type_icon', $iconFile );
  
}

Anyone have an idea where does this behavior come from? is there a better hook to trigger taxonomy creation as it sound to me create & created hooks reset $_FILES?

0

This is just a half answer, but I wanted to share my findings.

When adding a new term via the Add Term screen /wp-admin/edit-tags.php?taxonomy=work_type&post_type=work, AJAX is being used and it is not handling the $_FILES array, so it is empty.

If you disable JS in your browser temporarily, then reload the page, add a new tag along with an image, then you can see that $_FILES is now populated when debugging.

The AJAX is being handled using \wp-admin\js\tags.js. Looking further in here, we can see that the $_FILES array is not being sent over with $.post(ajaxurl, $('#addtag').serialize(), function(r).

I'm not sure of the best way to get around this. One way would be to unwire WP's AJAX handler, then create your own using the original handler as a starting point, but this is really not ideal.

While researching, I came across the Taxonomy Images plugin, which takes a different approach, by adding a UI next to each added term that allows you to upload an image.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.