0

I have created a custom metabox that contains a wp_editor. The wp_editor shows content of a uploaded .csv file and constructs a table. I want to allow the users to edit the table after uploading the .csv file.

This works. Now I want to make it a little bit more advanced.
So I added the possibility to upload multiple .csv files.
This also works. The problem is the saving of the wp_editors.
wp_editor states that every editor should have it's own editor_id.
see: http://codex.wordpress.org/Function_Reference/wp_editor

I can add a number??? to the editor_id but the problem is saving/updating the content.
I do this with a save function.
See below what I did.
This is the foreach loop checking how many .csv files there are and constructs a wp_editor everytime it comes across one:

$value = get_post_meta($post->ID, 'mcu', true);
$get_url = unserialize ($value);
foreach ($get_url as $list) {
        echo '<p style="margin:50px 0 0 0;">';
        echo '<strong>'.$list.'</strong>';
        ob_start();
        jj_readcsv($list,true);
        $link = ob_get_contents();
        ob_end_clean();
        $editor_id = 'my_uploaded_csv';

        wp_editor( $link, $editor_id );
        echo '</p>';
    }

Here is a basic save function for the wp_editor. I can't seem to figure out how I can make this have a foreach loop to so I can save/update all the data.

function save_wp_editor_fields(){
    global $post;
    update_post_meta($post->ID, 'my_uploaded_csv', $_POST['my_uploaded_csv']);
}

I hope anyone can assist me with this.

M.

1 Answer 1

0

wp_editor function accepts (array) $settings param and that one can contain textarea_name as a key. If it's not being specified, it's set by default as $editor_id. See: https://github.com/WordPress/WordPress/blob/c392ff6f90d550bad876d8a984ad6b3b8a49cd96/wp-includes/class-wp-editor.php#L44

So you might want to do something like this:

foreach ($get_url as $list) {
    ...
    $settings = array( 'textarea_name' => 'uploaded_csv[]' );
    ...
    wp_editor( $link, $editor_id, $settings );
    ...
}

And in your saving function, you simply loop over the $_POST['uploaded_csv'] array

function save_wp_editor_fields(){
    global $post;
    foreach ( $_POST['uplaoded_csv'] as $key => $uploaded_csv ) {
        update_post_meta($post->ID, 'my_uploaded_csv_'.$key, $uploaded_csv);
    }
}

Of course you should figure out the uniqueness of the created meta (eg. giving it a number or handle more meta keys per post with non unique name. See http://codex.wordpress.org/Function_Reference/add_post_meta).

Hope this helps a bit!

1
  • Yes that worked. Thnx for the explanation also. It's always nice to understand what you're doing. FYI: I gave the editor_id a number. Commented Sep 24, 2014 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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