問題描述

我最終想要實現的是額外的設置添加到圖像詳細信息框,將存儲在圖像<img> 標記為 data-*屬性

示例:<img src="..." data-my_setting="...">


我的代碼

我正在創建一個插件,我需要創建更多的設置,當你編輯圖像。到目前為止,我有以下代碼:

jQuery(function($) {

    var imageDetails = wp.media.view.ImageDetails

    wp.media.view.ImageDetails = wp.media.view.ImageDetails.extend({
        // Initialize - Call function to add settings when rendered
        initialize: function() {
            this.on('post-render', this.add_settings);
        },
        // To add the Settings
        add_settings: function() {
            $('.advanced-section').prepend('
                <h2>My Settings</h2>
                <input type="text" class="my_setting">
            ');

            // Set Options
            this.controller.image.set({"data-settings": 'setting-value-here'})
        }
    });

}) // End of jQuery(function($))

我創建了一個新的帖子並添加了一個圖像,然後點擊它並按下 Edit(工具欄中彈出的鉛筆圖標) 。我最終在圖片詳細信息頁面,這是什麼樣子:

到現在為止還挺好。在這行:

this.controller.image.set({"data-settings": 'setting-value-here'})

我通常會使用 jQuery 獲取輸入的值,但為了測試目的,我將其改為'setting-value-here'的靜態值。我在圖像詳細信息框的 bottom-right 角中按了’Update’ 。


問題

在文本編輯器中,它顯示 HTML 代碼:

這個沒有 data-settings="setting-value-here",怎麼來的?

如果我用以下替代行:

 this.controller.image.set({alt: 'setting-value-here'})

它確實將 ALT 標籤更改為 alt="setting-value-here"。那麼我試圖設置 data- *屬性我做錯了什麼?


解決方案

感謝 @bonger(擁有 50 個聲望的完整賞金),我有以下代碼:

PHP:

function add_my_settings() {
    ob_start();
    wp_print_media_templates();
    $tpl = ob_get_clean();
    if ( ( $idx = strpos( $tpl, 'tmpl-image-details' ) ) !== false
            && ( $before_idx = strpos( $tpl, '<div class="advanced-section">', $idx ) ) !== false ) {
        ob_start();
        ?>
        <div class="my_setting-section">
            <h2><?php _e( 'My Settings' ); ?></h2>
            <div class="my_setting">
                <label class="setting my_setting">
                    <span><?php _e( 'My Setting' ); ?></span>
                        <input type="text" data-setting="my_setting" value="{{ data.model.my_setting }}" />
                    </label>
                </div>
            </div>
        <?php
        $my_section = ob_get_clean();
        $tpl = substr_replace( $tpl, $my_section, $before_idx, 0 );
    }
    echo $tpl;
};

// Hack the output of wp_print_media_templates()
add_action('wp_enqueue_media', $func =
    function() {
        remove_action('admin_footer', 'wp_print_media_templates');
        add_action('admin_footer',  'add_my_settings');
    }
);

JavaScript :(需要使用 wp_enqueue_script()入隊)

// When Image is Edited
wp.media.events.on('editor:image-edit', function(data) {
    data.metadata.my_setting = data.editor.dom.getAttrib( data.image, 'data-my_setting' );
});

// When Image is Updated
wp.media.events.on('editor:image-update', function(data) {
    data.editor.dom.setAttrib( data.image, 'data-my_setting', data.metadata.my_setting );
});

最佳解決方案

一個方法是使用 (非常方便) 的編輯器:image-edit 和編輯器:image-update 事件觸發由 tinymce wpeditimage 插件直接獲取/設置 dom(更新為包裝在 wp_enqueue_media 動作):

add_action( 'wp_enqueue_media', function () {
    add_action( 'admin_footer', function () {
        ?>
        <script type="text/javascript">
        jQuery(function ($) {
            if (wp && wp.media && wp.media.events) {
                wp.media.events.on( 'editor:image-edit', function (data) {
                    data.metadata.my_setting = data.editor.dom.getAttrib( data.image, 'data-my_setting' );
                } );
                wp.media.events.on( 'editor:image-update', function (data) {
                    data.editor.dom.setAttrib( data.image, 'data-my_setting', data.metadata.my_setting );
                } );
            }
        });
        </script>
        <?php
    }, 11 );
} );

要添加和填充設置字段,可能會破壞 wp_print_media_templates()的輸出,而不是覆蓋 ImageDetails.initialize()(更新為包裝在 wp_enqueue_media 操作中):

add_action( 'wp_enqueue_media', function () {
    remove_action( 'admin_footer', 'wp_print_media_templates' );
    add_action( 'admin_footer', $func = function () {
        ob_start();
        wp_print_media_templates();
        $tpl = ob_get_clean();
        // To future-proof a bit, search first for the template and then for the section.
        if ( ( $idx = strpos( $tpl, 'tmpl-image-details' ) ) !== false
                && ( $before_idx = strpos( $tpl, '<div class="advanced-section">', $idx ) ) !== false ) {
            ob_start();
            ?>
    <div class="my_setting-section">
        <h2><?php _e( 'My Settings' ); ?></h2>
        <div class="my_setting">
            <label class="setting my_setting">
                <span><?php _e( 'My Setting' ); ?></span>
                <input type="text" data-setting="my_setting" value="{{ data.model.my_setting }}" />
            </label>
        </div>
    </div>
            <?php
            $my_section = ob_get_clean();
            $tpl = substr_replace( $tpl, $my_section, $before_idx, 0 );
        }
        echo $tpl;
    } );
} );

參考文獻

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