我正在创建一个元盒来上载PDF文件。
对于这一行代码,我有以下警告通知:$this_file = $filearray[\'url\'];
警告:中的字符串偏移量“url”非法
完整的功能代码:
function wp_custom_attachment() {
wp_nonce_field( plugin_basename(__FILE__), \'wp_custom_attachment_nonce\' );
$html = \'<p class="description">Upload your PDF here.</p>\';
$html .= \'<input id="wp_custom_attachment" name="wp_custom_attachment" size="25" type="file" value="" />\';
$filearray = get_post_meta( get_the_ID(), \'wp_custom_attachment\', true );
$this_file = $filearray[\'url\'];
if ( $this_file != \'\' ) {
$html .= \'<div><p>Current file: \' . $this_file . \'</p></div>\';
}
echo $html;
}
提前感谢您的帮助。编辑
function add_custom_meta_boxes() {
add_meta_box(
\'wp_custom_attachment\',
\'PDF File\',
\'wp_custom_attachment\',
\'workshop\',
\'normal\'
) ;
}
add_action( \'add_meta_boxes\', \'add_custom_meta_boxes\' );
function wp_custom_attachment() {
wp_nonce_field( plugin_basename(__FILE__), \'wp_custom_attachment_nonce\' );
$html = \'<p class="description">Upload your PDF here.</p>\';
$html .= \'<input id="wp_custom_attachment" name="wp_custom_attachment" size="25" type="file" value="" />\';
$filearray = get_post_meta( get_the_ID(), \'wp_custom_attachment\', true );
$this_file = $filearray[\'url\'];
if ( $this_file != \'\' ) {
$html .= \'<div><p>Current file: \' . $this_file . \'</p></div>\';
}
echo $html;
}
function save_custom_meta_data( $id ) {
if ( ! empty( $_FILES[\'wp_custom_attachment\'][\'name\'] ) ) {
$supported_types = array( \'application/pdf\' );
$arr_file_type = wp_check_filetype( basename( $_FILES[\'wp_custom_attachment\'][\'name\'] ) );
$uploaded_type = $arr_file_type[\'type\'];
if ( in_array( $uploaded_type, $supported_types ) ) {
$upload = wp_upload_bits($_FILES[\'wp_custom_attachment\'][\'name\'], null, file_get_contents($_FILES[\'wp_custom_attachment\'][\'tmp_name\']));
if ( isset( $upload[\'error\'] ) && $upload[\'error\'] != 0 ) {
wp_die( \'There was an error uploading your file. The error is: \' . $upload[\'error\'] );
} else {
add_post_meta( $id, \'wp_custom_attachment\', $upload );
update_post_meta( $id, \'wp_custom_attachment\', $upload );
}
}
else {
wp_die( "The file type that you\'ve uploaded is not a PDF." );
}
}
}
add_action( \'save_post\', \'save_custom_meta_data\' );
function update_edit_form() {
echo \' enctype="multipart/form-data"\';
}
add_action( \'post_edit_form_tag\', \'update_edit_form\' );