我已经在“二十一五”主题的新安装上尝试了这一点。我正在向允许上载的页面添加metabox-每当我在新页面上载文件时,它会上载该文件,但也会使用post_id
作为名称。然后将幻像ID保存为post meta。如果我先发布帖子,然后上传文件,一切正常。
这是我的save_post
hook,下面的代码是最低限度的,没有验证,只是为了复制问题。
/**
* Save Metaboxes
* @param int $post_id
*/
function save_custom_meta_boxes( $post_id ) {
// If we\'re not in the right place, bailout
if( ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) || ! isset( $post_id ) || ! isset( $_POST[\'post_type\'] ) || ! current_user_can( \'edit_post\', $post_id ) ) {
return;
}
if( isset( $_FILES[\'_uploaded_file\'] ) && ! empty( $_FILES[\'_uploaded_file\'][\'name\'] ) ) {
require_once( ABSPATH . \'wp-admin/includes/image.php\' );
$uploadStatus = wp_handle_upload( $_FILES[\'_uploaded_file\'], array( \'test_form\' => false ) );
$fileID = wp_insert_attachment( array(
\'post_mime_type\' => $uploadStatus[\'type\'],
\'post_title\' => preg_replace( \'/\\.[^.]+$/\', \'\', basename( $uploadStatus[\'file\'] ) ),
\'post_content\' => \'\',
\'post_status\' => \'inherit\'
),
$uploadStatus[\'file\'],
$post_id
);
$attachmentData = wp_generate_attachment_metadata( $fileID, $uploadStatus[\'file\'] );
wp_update_attachment_metadata( $fileID, $attachmentData );
update_post_meta( $post_id, \'_uploaded_file\', $fileID );
}
}
add_action( \'save_post\', \'save_custom_meta_boxes\' );
Metabox - 这是metabox的显示。
/** `page_meta` Callback Function **/
function page_meta_cb( $post ) {
wp_nonce_field( \'page_meta_metabox\', \'page_meta_nonce\' );
$uploaded_file_id = get_post_meta( $post->ID, \'_uploaded_file\', true );
?>
<table style="width:100%;">
<tbody>
<tr id="uploaded_file">
<td style="width:8%;min-width:105px;">
<label for="uploaded_file_input" style="font-weight:bold;">Uploaded File</label><br />
</td>
<td>
<input type="file" style="width:100%;" name="_uploaded_file" value="" id="uploaded_file_input" />
<?php if( is_numeric( $uploaded_file_id ) ) : ?>
<div class="fileLink">
<a href="post.php?post=<?php echo $uploaded_file_id; ?>&action=edit" target="_blank">Edit File - <?php echo get_the_title( $uploaded_file_id ); ?></a>
<br />
<br />
</div>
<?php endif; ?>
</td>
</tr>
</tbody>
</table>
<?php
} // END Metabox
媒体库如下所示:
同样,问题只在上传到新帖子时出现——已发布的帖子在上传时没有问题。问题可能是什么?
最合适的回答,由SO网友:cybmeta 整理而成
我对您的代码进行了一些修改,以检查帖子类型是否为页面,当前用户是否可以编辑该页面,验证nonce和插入附件功能。生成的代码正在运行。似乎不检查帖子类型可能是问题的原因。此外,您不需要手动包含“wp admin/includes/image”。php’:
add_filter(\'post_edit_form_tag\', function() {
echo \' enctype="multipart/form-data"\';
});
add_action( \'save_post\', \'save_custom_meta_boxes\', 10, 2 );
function save_custom_meta_boxes( $post_id, $post ) {
// If we\'re not in the right place, bailout
if( ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) || ! isset( $post_id ) || $post->post_type !== \'page\' || ! current_user_can( \'edit_page\', $post_id ) ) {
return;
}
if ( !isset( $_POST[\'page_meta_nonce\'] ) || ! wp_verify_nonce( $_POST[\'page_meta_nonce\'], \'page_meta_metabox\' ) ) {
return;
}
if( ! empty( $_FILES[\'_uploaded_file\'][\'name\'] ) ) {
$uploadStatus = wp_handle_upload( $_FILES[\'_uploaded_file\'], array( \'test_form\' => false ) );
if ( $uploadStatus && ! isset( $uploadStatus[\'error\'] ) ) {
$fileID = wp_insert_attachment( array(
\'post_mime_type\' => $uploadStatus[\'type\'],
\'post_title\' => preg_replace( \'/\\.[^.]+$/\', \'\', basename( $uploadStatus[\'file\'] ) ),
\'post_content\' => \'\',
\'post_status\' => \'inherit\'
),
$uploadStatus[\'file\'],
$post_id
);
if( $fileID ) {
$attachmentData = wp_generate_attachment_metadata( $fileID, $uploadStatus[\'file\'] );
wp_update_attachment_metadata( $fileID, $attachmentData );
update_post_meta( $post_id, \'_uploaded_file\', $fileID );
}
}
}
}
add_action( \'add_meta_boxes_page\', \'page_meta\' );
function page_meta() {
add_meta_box("page_upload_image", \'Upload image\', \'page_meta_cb\');
}
/** `page_meta` Callback Function **/
function page_meta_cb( $post ) {
wp_nonce_field( \'page_meta_metabox\', \'page_meta_nonce\' );
$uploaded_file_id = get_post_meta( $post->ID, \'_uploaded_file\', true );
?>
<div>
<table style="width:100%;">
<tbody>
<tr id="uploaded_file">
<td style="width:8%;min-width:105px;">
<label for="uploaded_file_input" style="font-weight:bold;">Uploaded File</label><br />
</td>
<td>
<input type="file" style="width:100%;" name="_uploaded_file" value="" id="uploaded_file_input" />
<?php if( is_numeric( $uploaded_file_id ) ) : ?>
<div class="fileLink">
<a href="post.php?post=<?php echo $uploaded_file_id; ?>&action=edit" target="_blank">Edit File - <?php echo get_the_title( $uploaded_file_id ); ?></a>
<br />
<br />
</div>
<?php endif; ?>
</td>
</tr>
</tbody>
</table>
</div>
<?php
} // END Metabox