繁体   English   中英

TinyMCE弹出窗口添加媒体按钮

[英]TinyMCE popup add media button

在我正在开发的Wordpress主题中,我有一个TinyMCEPopup来向编辑器添加短代码,一些短代码需要图像。 我可以添加一个“添加媒体”按钮,打开Wordpress媒体上传器,即使我在TinyMCEPopup中,也允许用户选择或上传图像吗?

不知道它是否会有所帮助,但我遇到了同样的问题并解决了这个问题。

functions.php添加

add_action( 'after_setup_theme', 'mytheme_theme_setup' );

if ( ! function_exists( 'mytheme_theme_setup' ) ) {
    function mytheme_theme_setup() {

        add_action( 'init', 'mytheme_buttons' );

    }
}

/********* TinyMCE Buttons ***********/
if ( ! function_exists( 'mytheme_buttons' ) ) {
    function mytheme_buttons() {
        if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
            return;
        }

        if ( get_user_option( 'rich_editing' ) !== 'true' ) {
            return;
        }

        add_filter( 'mce_external_plugins', 'mytheme_add_buttons' );
        add_filter( 'mce_buttons', 'mytheme_register_buttons' );
    }
}

if ( ! function_exists( 'mytheme_add_buttons' ) ) {
    function mytheme_add_buttons( $plugin_array ) {
        $plugin_array['mybutton'] = get_template_directory_uri().'/js/tinymce_buttons.js';
        return $plugin_array;
    }
}

if ( ! function_exists( 'mytheme_register_buttons' ) ) {
    function mytheme_register_buttons( $buttons ) {
        array_push( $buttons, 'mybutton' );
        return $buttons;
    }
}

这将初始化您需要的按钮。 现在在tinymce_buttons.js你会添加类似的东西

(function() {
    tinymce.PluginManager.add('mybutton', function( editor, url ) {
        editor.addButton( 'mybutton', {
            text: 'My button for media upload',
            icon: false,
            onclick: function() {
                editor.windowManager.open( {
                    title: 'Insert Media',
                    body: [
                        {
                            type: 'textbox',
                            name: 'img',
                            label: 'Image',
                            value: '',
                            classes: 'my_input_image',
                        },
                        {
                            type: 'button',
                            name: 'my_upload_button',
                            label: '',
                            text: 'Upload image',
                            classes: 'my_upload_button',
                        },
                    ],
                    onsubmit: function( e ) {
                        editor.insertContent( '[shortcode-name img="' + e.data.img + '"]');
                    }
                });
            },
        });
    });

})();

jQuery(document).ready(function($){
    $(document).on('click', '.mce-my_upload_button', upload_image_tinymce);

    function upload_image_tinymce(e) {
        e.preventDefault();
        var $input_field = $('.mce-my_input_image');
        var custom_uploader = wp.media.frames.file_frame = wp.media({
            title: 'Add Image',
            button: {
                text: 'Add Image'
            },
            multiple: false
        });
        custom_uploader.on('select', function() {
            var attachment = custom_uploader.state().get('selection').first().toJSON();
            $input_field.val(attachment.url);
        });
        custom_uploader.open();
    }
});

首先添加按钮功能。 此处给出了弹出窗口中的选项列表。 它不是100%完整,但比废话的官方文档更好。

第一部分是按钮初始化。 这为您提供了“我的媒体上传按钮”按钮,点击后您将获得一个带有输入字段和按钮的模态。

单击按钮,将打开媒体上载,您可以选择图像。 在选择时,网址将位于输入字段中,您将在短代码中获取该网址。

希望能帮助到你 :)

还有另外一个问题( 来自tinymce插件弹出窗口的Open / Access WP媒体库 ),所以我在这里粘贴我的答案,因为这是类似的:

嗨 - 刚才我遇到了同样的问题并找到了解决方案,所以我在这里分享。 我希望现在还为时不晚。

首先要能够使用WP Add Media按钮,您必须将所需的脚本排入队列。 这很简单,只需调用wp_enqueue_media()函数:

add_action('admin_enqueue_scripts', 'enqueue_scripts_styles_admin');
function enqueue_scripts_styles_admin(){
    wp_enqueue_media();
}

此调用可确保您具有使用WP Media按钮所需的库。

当然,您还应该使用HTML元素来保存上​​传/选定的媒体文件URL,如下所示:

<input type="text" class="selected_image" />
<input type="button" class="upload_image_button" value="Upload Image">

第一个文本字段将保存媒体文件的URL,而第二个文本字段是用于打开媒体弹出窗口本身的按钮。

然后在你的jscript中,你会有这样的事情:

    var custom_uploader;

    $('.upload_image_button').click(function(e) {
        e.preventDefault();

        var $upload_button = $(this);

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

        //When a file is selected, grab the URL and set it as the text field's value
        custom_uploader.on('select', function() {
            var attachment = custom_uploader.state().get('selection').first().toJSON();
            $upload_button.siblings('input[type="text"]').val(attachment.url);
        });

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

    });

现在我不打算解释每一行,因为它并不难理解。 最重要的部分是使用wp对象使所有这些工作的部分。

棘手的部分是让所有这些工作在TinyMCE弹出窗口(这是我遇到的问题)。 我已经搜索过你的解决方案,这对我有用。 但在此之前,我会先谈谈我遇到的问题。 当我第一次尝试实现它时,我在弹出窗口本身遇到了“WP is undefined”问题。 要解决这个问题,您只需将WP对象传递给脚本,如下所示:

(function() {
tinymce.create('tinymce.plugins.someplugin', {
    init : function(ed, url) {
        // Register commands
        ed.addCommand('mcebutton', function() {
            ed.windowManager.open(
                {
                    file : url + '/editor_button.php', // file that contains HTML for our modal window
                    width : 800 + parseInt(ed.getLang('button.delta_width', 0)), // size of our window
                    height : 600 + parseInt(ed.getLang('button.delta_height', 0)), // size of our window
                    inline : 1
                },
                {
                    plugin_url : url,
                    wp: wp
                }
            );
        });

        // Register buttons
        ed.addButton('someplugin_button', {title : 'Insert Seomthing', cmd : 'mcebutton', image: url + '/images/some_button.gif' });
    }
});

// Register plugin
// first parameter is the button ID and must match ID elsewhere
// second parameter must match the first parameter of the tinymce.create() function above
tinymce.PluginManager.add('someplugin_button', tinymce.plugins.someplugin);

})();

我们感兴趣的是这一行=>“wp:wp”。 这一行确保我们将wp对象传递给弹出窗口(iframe真的......),当我们点击tinymce按钮时将打开它。 您实际上可以通过此对象(ed.windowManager.open方法的第二个参数)将任何内容传递给弹出窗口!

最后但并非最不重要的是你必须引用在你的javascript上传递wp对象,如下所示:

    var args = top.tinymce.activeEditor.windowManager.getParams();
    var wp = args.wp;

确保在调用/使用WP对象之前执行此操作。

这就是你要做的所有工作。 它对我有用,我希望它适合你:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM