一直在与一个bug斗争。在已创建的{taxonomy}钩子上,在已编辑的{taxonomy}钩子可以正常工作并上载所需的文件,没有问题。根据以下代码,我添加了自定义字段类型和自定义分类类型。工作正常。
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\',
)
);
}
根据下面的代码,在编辑和添加(new)到我的自定义分类法(work\\u type)上都添加enctype,效果很好,在这两种形式中都可见。
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"\';
}
根据以下代码,将上传文件输入字段添加到两个场景的管理面板添加(&A);编辑哪个效果很好。
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;
}
}
根据下面的代码,我试图使用wp upload函数来上传文件,这在编辑的{taxonomy}hook中非常有效。但是,它在created/create{taxonomy}hook&;中不起作用$_文件为空,文件[\'error\']=“quot;指定的文件未能通过上载测试"E;
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 );
}
有人知道这种行为是从哪里来的吗?是否有更好的钩子来触发分类法创建,正如我所认为的那样创建(&P);是否创建挂钩重置$\\u文件?