如果这对其他人有帮助,下面是我的代码实现这一点的样子。根据tbuteler的建议,我最终根本没有保存附件数据,但我发现我可以直接使用$附件数组,而不是使用$请求:
function my_attachment_fields_to_edit( $form_fields, $post ) {
$supported_exts = supported_types(); // array of mime types to show checkbox for
if ( in_array( $post->post_mime_type, $supported_exts ) ) {
// file is supported, show fields
$use_sc = true; // check box by default (false not to, or use other criteria)
$checked = ( $use_sc ) ? \'checked\' : \'\';
$form_fields[\'use_sc\'] = array(
\'label\' => \'Use SC\',
\'input\' => \'html\',
\'html\' => "<input type=\'checkbox\' {$checked} name=\'attachments[{$post->ID}][use_sc]\' id=\'attachments[{$post->ID}][use_sc]\' /> " . __("Insert shortcode instead of link", "txtdomain"),
\'value\' => $use_sc
);
}
return $form_fields;
}
function my_media_insert( $html, $id, $attachment ) {
if ( isset( $attachment[\'use_sc\'] ) && $attachment[\'use_sc\'] == "on" ) {
$output = \'[shortcode url="\'.$attachment[\'url\'].\'"]\';
return $output;
} else {
return $html;
}
}
// add checkbox to attachment fields
add_filter( \'attachment_fields_to_edit\', \'my_attachment_fields_to_edit\', null, 2 );
// insert shortcode if checkbox checked, otherwise default (link to file)
add_filter( \'media_send_to_editor\', \'my_media_insert\', 20, 3 );
EDIT 1/2013: This solution no longer works in WordPress 3.5+. 附件屏幕上的复选框发送AJAX请求以保存附件元数据。鉴于此,似乎再也没有办法将复选框仅仅用作附件屏幕上的“标志”。如果有人找到另一个解决方案,我会很高兴地更改已批准的答案!