Show dialog box in TinyMCE plugin and use WordPress php functions in it

I’m adding a TinyMCE button to my plugin. So far, I’ve been able to add the button and call my editor_plugin.js file and it’s command. Not an easy mission btw…

I want this button to show a Dialog box, which gives the user several options. These are dropdown boxes which I need to fill and some text boxes for the user to just write the input. Some of them needs info from WordPress functions, and others are just static info.

After this dialog box is accepted, I want it to write the shortcode on the post for my plugin to display with the options selected in the box.

The thing is, I want to use some of WordPress’ functions in my php file. But it’s sort of “out of context”. I can’t use WordPress functions, or even the __() functions for i18n.

Here’s the code I’m using from one of the TinyMCE examples:

ed.addCommand('mceExample', function() {
                            ed.windowManager.open({
                                    file : url + '/box.php',
                                    width : 500 + ed.getLang('example.delta_width', 0),
                                    height : 300 + ed.getLang('example.delta_height', 0),
                                    inline : 1
                            }, {
                                    plugin_url : url, // Plugin absolute URL
                                    some_custom_arg : 'custom arg' // Custom argument
                            });
                    });

I’m also missing the way to actually write into the post. As you can see here, this code uses nothing to write into the post. I tried using this code I found on another example: ed.execCommand('mceInsertContent', false, 'Hello World'); but haven’t found out how to use it on my new script.

Sorry if this has been answered before, but all the documentation regarding TinyMCE seams pretty confusing, and I’ve had a hard time integrating this into my plugins. Thanks in advance.

2 Answers
2

Instead of requireing admin.php you just can use WP built-in ajax functionality, even if it’s not ajax in this case.

Add a hook

 add_action('wp_ajax_my_plugin_function', 'my_plugin_function_callback');

Create your output function (callback)

function my_plugin_function_callback() {
    // do stuff
}

Call it this way

instead of:

file : url + '/box.php',

you should be able to use the global ajaxurl:

file: ajaxurl + '?action=my_plugin_function&optional=data ...

Maybe add a nonce for security reasons, but that’s up to you 🙂

Leave a Comment