3

WordPressのメディアアップローダーを使用してアップロードされた画像の場合、その画像のショートコードをエディターに送信する[投稿に挿入]ボタンがあります。

ユーザーが画像を選択してファイルのURLをテキスト入力に送信できるように、フォーカスされたときにメディアアップローダーを表示するテキスト入力があります。

私が抱えている主な問題は、ファイルのURLを適切なテキストフィールドに送信する追加の[投稿に挿入]ボタンを作成することです。

そのためにどのフックを使用し、ファイルのURLデータを入力フィールドに返すにはどうすればよいですか?

あなたの指導は大歓迎です!

4

2 に答える 2

3

あなたが説明したのは、それを行う古いWordpressの方法です...Wordpress 3.5+で新しいアップローダーを使用する場合はwp.media、wp-admin/js/customのコードと同様に、オブジェクトを作成してアップロードできます-background.js:

           // Create the media frame.
            frame = wp.media.frames.customBackground = wp.media({
                // Set the title of the modal.
                title: $el.data('choose'),

                // Tell the modal to show only images.
                library: {
                    type: 'image'
                },

                // Customize the submit button.
                button: {
                    // Set the text of the button.
                    text: $el.data('update'),
                    // Tell the button not to close the modal, since we're
                    // going to refresh the page when the image is selected.
                    close: false
                }
            });

            // When an image is selected, run a callback.
            frame.on( 'select', function() {
                // Grab the selected attachment.
                var attachment = frame.state().get('selection').first();

                // Run an AJAX request to set the background image.
                $.post( ajaxurl, {
                    action: 'set-background-image',
                    attachment_id: attachment.id,
                    size: 'full'
                }).done( function() {
                    // When the request completes, reload the window.
                    window.location.reload();
                });
            });

            // Finally, open the modal.
            frame.open();

ファイルが選択されると、frame.on('select' function(){コードが実行されます。

于 2013-01-09T05:14:54.220 に答える
1

さらに検索すると、これを行う方法を説明する良い情報源を見つけることができました。私はJavaScriptとPHPのミックスに行きました:

JavaScript

$j('input').live('focusin',function(){

    var target = '#'+$j(this).attr('id');

    tb_show('','media-upload.php?post_id=[post_id]&tab=gallery&context=choose&TB_iframe=1');

    window.send_to_editor = function(html) {            

        fileurl = $j(html).attr('href');
        $j(target).val(fileurl);
        tb_remove();

    };

});

ソース: http://jaspreetchahal.org/wordpress-using-media-uploader-in-your-plugin/

PHP

/* Customize button */

function media_uploader_btn($form_fields, $post) {

    $send = "<input type='submit' class='button' name='send[$post->ID]' value='" . esc_attr__( 'Choose This File' ) . "' />";

    $form_fields['buttons'] = array('tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>$send</td></tr>\n");
    $form_fields['context'] = array( 'input' => 'hidden', 'value' => 'choose' );

    return $form_fields;

}

/* Check for button context */

function check_upload_image_context($context){

    if(isset($_REQUEST['context']) && $_REQUEST['context'] == $context){

        return true;

    } elseif(isset($_POST['attachments']) && is_array($_POST['attachments'])){

        /* check for context in attachment objects */

        $image_data = current($_POST['attachments']);

        if (isset($image_data['context']) && $image_data['context'] == $context ){

            return true;

        }

    }

    return false;
}


if(check_upload_image_context('choose')){

    add_filter('attachment_fields_to_edit', 'media_uploader_btn', 20, 2);

}

ソース: http://shibashake.com/wordpress-theme/how-to-hook-into-the-media-upload-popup-interface

于 2012-11-08T07:06:49.747 に答える