web-dev-qa-db-ja.com

保存の前後にメタボックスに値を追加する

ファイルのアップロードをクリックしたら、ファイルを選択してからファイルの選択をクリックします。 [更新]をクリックする前または後に[ファイルのアップロード]をもう一度クリックして、選択したファイルを "my_file_URL"および "my_file_ID"メタボックスの既存の値に追加できるようにしたいです。しかし、私はこれをやろうとする方法にいくつかの困難を抱えています。今それは以前の値を削除します。任意の助けは大歓迎です。ありがとうございました。コードは以下のとおりです。

functions.php

function add_resource($post){
$url = get_post_meta($post->ID, 'my_file_URL', true);
$id = get_post_meta($post->ID, 'my_file_ID', true);

?>

 <input id="my_file_URL" name="my_file_URL" type="text"
     value="<?php echo $url; ?>" style="width:400px;" />
 <input id="my_file_ID" name="my_file_ID" type="hidden"
     value="<?php echo $id; ?>" style="width:400px;" />
 <input id="my_upl_button" type="button" value="Upload File" /><br/>

 <script>
jQuery(document).ready(function($){

    var custom_uploader;

     $('#my_upl_button').click(function(e) {

        e.preventDefault();

       //If the uploader object has already been created, reopen the dialog
        if (custom_uploader) {
            custom_uploader.open();
            return;
        } 

        //Extend the wp.media object
        custom_uploader = wp.media.frames.file_frame = wp.media({
            title: 'Choose File',
            button: {
                text: 'Choose File'
            },
            multiple: true
        });

        //When a file is selected, grab the URL and set it as the text field's value
        custom_uploader.on('select', function() {
                var selection = custom_uploader.state().get('selection').toJSON();

                var urls = selection.map( function(attachment){
                    return attachment.url;
                });
                var ids = selection.map( function(attachment){
                    return attachment.id;
                });

                for(i=0; i < selection.length; i++){

                        $( '#my_file_URL' ).after(
                            '<br/><p>File URL: ' + selection[i].url + '</p><p>  Description: ' + selection[i].description + '</p><br/>'
                        );
                }

                $( '#my_file_URL' ).val(urls.join(','));
                $( '#my_file_ID' ).val(ids.join(','));


            });


        //Open the uploader dialog
        custom_uploader.open();

    });

});
<?php
}

function save_meta_box($post_id){

    global $post;


            if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){
                return;
            } else {
                if (isset($_POST['my_file_URL'])){

                    if(is_array($url))
                        $url[] = $new_url;
                    else
                        $url = array($new_url);

                    update_post_meta($post_id, 'my_file_URL', $_POST['my_file_URL']);

                }


                if (isset($_POST['my_file_ID'])){

                    if(is_array($id))
                        $id[] = $new_id;
                    else
                        $id = array($new_id);

                    update_post_meta($post_id, 'my_file_ID', $_POST['my_file_ID']);

                }

            }


}
add_action('save_post','save_meta_box');

single.php

               $url = get_post_meta($post->ID, 'my_file_URL', true);

               $id = get_post_meta($post->ID, 'my_file_ID', true);

               $urls = explode(',', $url);

               $urlCount = count($urls);

               $ids = explode(',', $id);

              for($i=0; $i < $urlCount; $i++){ 

                 $fileID = $ids[$i];

                 $attachment_filesize = size_format( filesize( get_attached_file( $fileID ) ), 2 );

                 $date = get_the_date('M d, Y', $fileID);

                 $attachment = get_post( $fileID ); ?>

                  <div class="codesc" style="float:left;"><h3><a href="<?php echo $urls[$i] ?>" target="_blank"><?php echo $attachment->post_title; ?><br>
                    <span class="linkdetails"><?php echo $attachment_filesize; ?> | <?php echo $date; ?></span></a></h3>
                    <p><?php echo $attachment->post_content; ?></p>
                  </div>

            <?php

              } ?>
2
Peter G

これを追加することで動作させることができました...

jQuery(document).ready(function($){

    var custom_uploader;
    var currentfiles = document.getElementById('my_file_URL').value;
    var currentID = document.getElementById('my_file_ID').value;

.....

                var urls = selection.map( function(attachment){
                    return attachment.url;
                });
                var ids = selection.map( function(attachment){
                    return attachment.id;
                });


                $( '#my_file_URL' ).val(urls.join(','));
                $( '#my_file_ID' ).val(ids.join(','));

                if(currentfiles != ''){
                    $( '#my_file_URL' ).val(currentfiles + "," + urls);
                }

                if(currentID != ''){
                    $( '#my_file_ID' ).val(currentID + "," + ids);
                }
1
Peter G