<?php
//Formular für Dateiupload vorbereiten
add_action ( 'post_edit_form_tag', 'cpt_edit_form_tag' );
function cpt_edit_form_tag() {
echo ' enctype="multipart/form-data"';
}
//Metabox
add_action ( 'add_meta_boxes', 'cpt_add_custom_box' );
function cpt_add_custom_box() {
add_meta_box ( 'cpt_id', 'Dummy', 'dummy_formular_box', 'dummy', 'normal', 'high' );
}
/* Hier werden die Customs Fields definiert */
function dummy_formular_box() {
global $post;
wp_nonce_field ( plugin_basename ( __FILE__ ), 'dummy_nonce' );
?>
<br><label for="Text1">Text: </label>
<?php $text=get_post_meta(get_the_ID(), '_text',true);?>
<input type="text" id="dummy_text" name="text"
value="<?php echo $text;?>" />
<br />
<?php
$pdfurl = get_post_meta( get_the_ID(), 'pdfa', true );
$pdf = $pdfurl['url'];
?>
<label>PDF:</label><input type="file" name="pdf_file" value="<?php echo $pdf; ?>" /><a href="<?php echo $pdf; ?>" target="_blank">Download</a><br /><?php echo $pdf; ?>
<?php }
//Speichern der Daten
add_action ( 'save_post', 'cpt_save' );
function cpt_save($post_id) {
global $wpdb;
global $post;
if (defined ( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE)
return;
if (! wp_verify_nonce ( $_POST ['dummy_nonce'], plugin_basename ( __FILE__ ) ))
return;
if ('page' == $_POST ['dummy']) {
if (! current_user_can ( 'edit_page', $post_id ))
return;
} else {
if (! current_user_can ( 'edit_post', $post_id ))
return;
}
/* Setzen der Variablen */
$text = $_POST ['text'];
// FILE-UPLOAD
if(!empty($_FILES['pdf_file']['name'])) {
$supported_types = array('application/pdf');
$arr_file_type = wp_check_filetype(basename($_FILES['pdf_file']['name']));
$uploaded_type = $arr_file_type['type'];
if(in_array($uploaded_type, $supported_types)) {
$upload = wp_upload_bits($_FILES['pdf_file']['name'], null, file_get_contents($_FILES['pdf_file']['tmp_name']));
if(isset($upload['error']) && $upload['error'] != 0) {
wp_die('Ein Fehler ist beim hochldane aufgetreten. Fehler: ' . $upload['error']);
} else {
add_post_meta($post_id, 'pdfa', $upload);
update_post_meta($post_id, 'pdfa', $upload);
}
}
else {
wp_die("Sie haben keine gueltige Datei hochgeladen!!");
}
}
/* Übergabe der Variablen */
update_post_meta ( $post_id, '_text', $text );
/* Der Titel wird automatisch gespeichert */
$title = $_POST ['text'];
$where = array (
'ID' => $post_id
);
$wpdb->update ( $wpdb->posts, array (
'post_title' => $title,
'post_name' => $title ), $where );
}
?>