简体   繁体   English

TinyMCE弹出窗口添加媒体按钮

[英]TinyMCE popup add media button

Inside a Wordpress theme I am developing, i've a TinyMCEPopup to add shortcode to the editor, some shortcode requires images. 在我正在开发的Wordpress主题中,我有一个TinyMCEPopup来向编辑器添加短代码,一些短代码需要图像。 Can i add an "Add media" button which opens the Wordpress media uploader and allow the user to select or upload an image even if i'm inside a TinyMCEPopup? 我可以添加一个“添加媒体”按钮,打开Wordpress媒体上传器,即使我在TinyMCEPopup中,也允许用户选择或上传图像吗?

Don't know if it will help, but I had the same issue and solved it like this. 不知道它是否会有所帮助,但我遇到了同样的问题并解决了这个问题。

In functions.php add 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;
    }
}

This will initialize the button you need. 这将初始化您需要的按钮。 Now in the tinymce_buttons.js you'll add something like 现在在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();
    }
});

First you add the button functionality. 首先添加按钮功能。 The list of options to put in popup is given here . 此处给出了弹出窗口中的选项列表。 It's not 100% complete, but better than the official documentation which is crap. 它不是100%完整,但比废话的官方文档更好。

The first part is the button initialization. 第一部分是按钮初始化。 This gives you a 'My button for media upload' button, and on click you should get a modal with input field and a button. 这为您提供了“我的媒体上传按钮”按钮,点击后您将获得一个带有输入字段和按钮的模态。

On button click the media upload will open and you can select your image. 单击按钮,将打开媒体上载,您可以选择图像。 On select the url will be in the input field, and you'll get it in your shortcode. 在选择时,网址将位于输入字段中,您将在短代码中获取该网址。

Hope it helps :) 希望能帮助到你 :)

There was another question like this ( Open/Access WP Media library from tinymce plugin popup window ) , so I'm pasting my answer here since this is similar: 还有另外一个问题( 来自tinymce插件弹出窗口的Open / Access WP媒体库 ),所以我在这里粘贴我的答案,因为这是类似的:

Hi - I had the same problem just now and found the solution so I'm sharing it here. 嗨 - 刚才我遇到了同样的问题并找到了解决方案,所以我在这里分享。 I hope it's not too late. 我希望现在还为时不晚。

First to be able to use WP Add Media button you would have to enqueue the needed script. 首先要能够使用WP Add Media按钮,您必须将所需的脚本排入队列。 This is easy, just call the wp_enqueue_media() function like so: 这很简单,只需调用wp_enqueue_media()函数:

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

This call ensures you have the needed libraries to use the WP Media button. 此调用可确保您具有使用WP Media按钮所需的库。

Of course you should also have the HTML elements to hold the uploaded/selected media file URL, something like this: 当然,您还应该使用HTML元素来保存上​​传/选定的媒体文件URL,如下所示:

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

The first text field will hold the URL of the media file while the second is a button to open the media popup window itself. 第一个文本字段将保存媒体文件的URL,而第二个文本字段是用于打开媒体弹出窗口本身的按钮。

Then in your jscript, you'd have something like this: 然后在你的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();

    });

Now I'm not going to explain every line because it's not that hard to understand. 现在我不打算解释每一行,因为它并不难理解。 The most important part is the one that uses the wp object to make all these to work. 最重要的部分是使用wp对象使所有这些工作的部分。

The tricky part is making all these work on a TinyMCE popup(which is the problem I faced). 棘手的部分是让所有这些工作在TinyMCE弹出窗口(这是我遇到的问题)。 I've searched hi and lo for the solution and here's what worked for me. 我已经搜索过你的解决方案,这对我有用。 But before that, I'll talk about what problem I encountered first. 但在此之前,我会先谈谈我遇到的问题。 When I first tried to implement this, I encountered the "WP is undefined" problem on the popup itself. 当我第一次尝试实现它时,我在弹出窗口本身遇到了“WP is undefined”问题。 To solve this, you just have to pass the WP object to the script like so: 要解决这个问题,您只需将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);

})();

What we're interested in is this line => "wp: wp" . 我们感兴趣的是这一行=>“wp:wp”。 This line ensures that we are passing the wp object to the popup window (an iframe really...) that is to be opened when we click the tinymce button. 这一行确保我们将wp对象传递给弹出窗口(iframe真的......),当我们点击tinymce按钮时将打开它。 You can actually pass anything to the popup window via this object (the 2nd parameter of the ed.windowManager.open method)! 您实际上可以通过此对象(ed.windowManager.open方法的第二个参数)将任何内容传递给弹出窗口!

Last but not the least you'd have to reference that passed wp object on your javascript like so: 最后但并非最不重要的是你必须引用在你的javascript上传递wp对象,如下所示:

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

Make sure you do that before calling/using the WP object. 确保在调用/使用WP对象之前执行此操作。

That's all you have to do to make this work. 这就是你要做的所有工作。 It worked for me, I hope it works for you :) 它对我有用,我希望它适合你:)

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

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