0

I have a custom metabox that let's the user upload .csv files while creating a page. The content of the .csv is shown on the created page at the "front-end".

I would like to show the content on the admin page in the WYSIWYG-editor so the user can change if necessary. Can anybody tell me how to view the content of a uploaded file?

I don't really think the code to the metabox and upload function is necessary but if anybody wants is let me know.

M.

  • You would display it the same way you would on the front-end, so you just need to know how to display a metabox, and save values? – Tom J Nowell Aug 18 '14 at 12:51
  • I know, and have it working, how to display a metabox, and save values. That's not the problem. The problem is that I don't know how to view content in the WYSIWYG-editor. Sorry if this wasn't clear – Interactive Aug 18 '14 at 12:53
  • ah you need to create a tinyMCE editor instance in your metabox? – Tom J Nowell Aug 18 '14 at 13:22
  • No almost :-) The metabox handles the upload of the .csv file. After the page is saved the editor should view the content of the .csv file. I found a function that show default text on a new post, but I can't get it to work on a page. If that would work I could use a if/else statement for the output of the content – Interactive Aug 18 '14 at 14:18
  • @TomJNowell I followed your idea and have created a tinyMCE editor in my metabox. I'm facing the following issue: The editor expects a string. However I have a csv. So I use fgetcsv to output the csv. The problem is that this isn't a string so it places the csv above the editor. Now I can't edit it :-(. Any thoughts? – Interactive Aug 19 '14 at 15:01
0

Okay.. I'm really losing it here.

I'm just gonna extract it all so it might be more clear.

In my functions.php:

function jj_readcsv($filename, $header=false) {
$handle = fopen($filename, "r");
echo '<table style="width:100%;">';
//display header row if true
if ($header) {
$csvcontents = fgetcsv($handle);
echo '<tr>';
foreach ($csvcontents as $headercolumn) {
    echo "<th>$headercolumn</th>";
}
echo '</tr>';
}
// displaying contents
while ($csvcontents = fgetcsv($handle)) {
echo '<tr>';
foreach ($csvcontents as $column) {
    echo "<td>$column</td>";
}
echo '</tr>';
}
echo '</table>';
fclose($handle);
}

This is the function that read's the uploaded file from the database and outputs it in a nice table.

Here is the custom metabox where the file is being uploaded to the server.

<?php 
function add_meta_box_csv() {

// Define the custom attachment for pages
add_meta_box(
    'meta_box_csv',
    'Upload csv',
    'meta_box_csv',
    'page',
    'normal'
);

} // end add_meta_box_csv
add_action('add_meta_boxes', 'add_meta_box_csv');

function meta_box_csv() {
$file = get_post_meta(get_the_ID(), 'meta_box_csv', true);
wp_nonce_field(plugin_basename(__FILE__), 'meta_box_csv_nonce');

$html = '<p class="description">';
$html .= 'Selecteer een .csv bestand.';
$html .= '</p>';
echo $html;  
printf( '<p><input type="file" name="meta_box_csv" value="'.$file['file'].'" size="15" /></p>' );


$content = jj_readcsv($file['file'],true);
//$editor_id = 'mycustomeditor';

//wp_editor( $content, $editor_id );
//$csv = array_map('str_getcsv', file('data.csv'));




} // end meta_box_csv

function save_custom_meta_data($id) {

/* --- security verification --- */
if(!wp_verify_nonce($_POST['meta_box_csv_nonce'], plugin_basename(__FILE__))) {
  return $id;
} // end if

if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  return $id;
} // end if

if('page' == $_POST['post_type']) {
  if(!current_user_can('edit_page', $id)) {
    return $id;
  } // end if
} else {
    if(!current_user_can('edit_page', $id)) {
        return $id;
    } // end if
} // end if
/* - end security verification - */

// Make sure the file array isn't empty
if(!empty($_FILES['meta_box_csv']['name'])) {

    // Setup the array of supported file types. In this case, it's just PDF.
    $supported_types = array('text/csv');
    //$supported_types = array('text/comma-separated-values');

    // Get the file type of the upload
    $arr_file_type = wp_check_filetype(basename($_FILES['meta_box_csv']['name']));
    $uploaded_type = $arr_file_type['type'];

    // Check if the type is supported. If not, throw an error.
    if(in_array($uploaded_type, $supported_types)) {

        // Use the WordPress API to upload the file
        $upload = wp_upload_bits($_FILES['meta_box_csv']['name'], null, file_get_contents($_FILES['meta_box_csv']['tmp_name']));

        if(isset($upload['error']) && $upload['error'] != 0) {
            wp_die('Er is een fout ontstaan bij het uploaden: ' . $upload['error']);
        } else {
            add_post_meta($id, 'meta_box_csv', $upload);
            update_post_meta($id, 'meta_box_csv', $upload);    
        } // end if/else

    } else {
        wp_die("Dit is geen .csv bestand");
    } // end if/else

  } // end if

} // end save_custom_meta_data
add_action('save_post', 'save_custom_meta_data');


function update_edit_form() {
echo ' enctype="multipart/form-data"';
} // end update_edit_form
add_action('post_edit_form_tag', 'update_edit_form');
?>

I can output the content of the uploades csv in my metabox. The problem is that I want to output the content of the csv in a wp_editor box, but to do that I need to have a string with data.

Is there a way where I can store the output data of function jj_readcsv() in a string variable so I can use it to output?

And considering what @TomJNowell said: I can't treat it like a plain text file.

I hope anyone understands my pain and can help me out!

Much appreciated. M.

-EDIT- Okay got it working. Thnx to: https://stackoverflow.com/users/803518/jlevett With his sollution on:https://stackoverflow.com/a/6675199/3115317 He gave me this:

ob_start();
jj_readcsv($file['file'],true);
$link = ob_get_contents();
ob_end_clean();
$editor_id = 'my_uploaded_csv';

wp_editor( $link, $editor_id );

And works like a charm.

0

Oke here we go again.
Now that I have the code working and the content is shown in the editor I'd like to save the content to the post.

The content is extracted from the uploaded .csv file so that's the only saved data I have. I's like to save the data in the database.

The upload function doesn't seems to be working. Here is what I have done but no luck:

ob_start();
jj_readcsv($file['file'],true);
$link = ob_get_contents();
ob_end_clean();
$editor_id = 'my_uploaded_csv';

wp_editor( $link, $editor_id );

Here I get the content from the uploades .csv and display it in the wp_editor to edit.
This is the save function:

function save_wp_editor_fields( $post_id ) {

if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

if( !current_user_can( 'edit_post' ) ) return;

if( isset( $_POST['save_wp_editor_fields'] ) )
    update_post_meta( $post_id, 'save_wp_editor_fields' ) );
}

add_action( 'save_post', 'save_wp_editor_fields' );

Where do I go wrong? M.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.