I'm creating a metabox to upload PDF files.
I have the following warning notice for this line of code: $this_file = $filearray['url'];
Warning: Illegal string offset ‘url’ in...
The complete function code:
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;
}
Thanks in advance for the help.
EDIT
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' );
wp_custom_attachment()
used? Is the current post always going to have awp_custom_attachment
field saved? Your code doesn't handle the possibility that there's no value. And if there is a value, what does it look like?['url']
inside$filearray
exist?if ( isset( $filearray ) && '' !== $filearray ) : $this_file = $filearray['url']; endif;