問題描述

我正在編寫一個自定義插件,我想添加”Add Media” 按鈕。

我只需要上傳媒體,而不是從上傳的文件中檢索任何內容/數據。

如何添加此按鈕?

謝謝

最佳解決辦法

如果要在管理面板中添加添加媒體按鈕:

你需要使用 wp_enqueue_media();

add_action ( 'admin_enqueue_scripts', function () {
    if (is_admin ())
        wp_enqueue_media ();
} );

然後使用這個 js:

jQuery(document).ready(function() {
    var $ = jQuery;
    if ($('.set_custom_images').length > 0) {
        if ( typeof wp !== 'undefined' && wp.media && wp.media.editor) {
            $(document).on('click', '.set_custom_images', function(e) {
                e.preventDefault();
                var button = $(this);
                var id = button.prev();
                wp.media.editor.send.attachment = function(props, attachment) {
                    id.val(attachment.id);
                };
                wp.media.editor.open(button);
                return false;
            });
        }
    }
});

使用這個 html:

<p>
    <input type="number" value="" class="regular-text process_custom_images" id="process_custom_images" name="" max="" min="1" step="1">
    <button class="set_custom_images button">Set Image ID</button>
</p>

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。