0

Im working on a plugin that loads a tinyMCE modal. As many who know this normally involves opening a static html or even php file. The problem I've run into, is that I would like to translate using WP I18n functions the many strings in my modal.

In the context of a piece of content that would normally be static, what options are there?

Below is a typical example of the javascript that registers a new tinymce plugin, creating a new button which triggers a modal.

(function() {
tinymce.create('tinymce.plugins.ExamplePlugin', {
    init : function(ed, url) {
        ed.addCommand('tinyExample', function() {
            ed.windowManager.open({
                file : url + '/example.htm', // THIS IS A TYPICAL PATH
                width : 450 + parseInt(ed.getLang('example.delta_width', 0)),
                height : 450 + parseInt(ed.getLang('example.delta_height', 0)),
                inline : 1
            }, {
                plugin_url : url
            });
        });
        ed.addButton('example', {title : 'Example', cmd : 'tinyExample', image: url + '/example.gif' });
    },
    getInfo : function() {
        return {
            longname : 'Example',
            author : 'Paul Robinson',
            authorurl : 'http://return-true.com',
            infourl : 'http://return-true.com',
            version : tinymce.majorVersion + "." + tinymce.minorVersion
        };
    }
});
tinymce.PluginManager.add('example', tinymce.plugins.ExamplePlugin);
})();

In this case the file url is a static html file. One option would be to instead use a php file and require the wp core like so:

// Load up Wordpress
define('WP_USE_THEMES', false);
require('../../../../../../wp-blog-header.php');

If we were to do this and then wrap all our strings in I18n functions, would this localise the strings in the modal for the user, and would this be best practice?

0

While I can't see any major fault in doing the way I proposed initially especially in relation to translation and localization, what I realised while researching this issue was that I was asking an ajax question. Basically what's the most efficient method to initialize the I18n components of wordpress. Short of making a custom ajax hook like I had above, I can leverage the ajax plugin api which has a number of benefits:

  1. Possibly faster as it may be loading less of WP core then directly bootstrapping WP
  2. Security as wp_ajax provides a structure helps keep me from doing something stupid
  3. We're not chasing the location of core files in the case of custom builds
  4. We also have the js global ajaxurl while in the admin

An implementation would look something like the following:

First we set up a function to output the processed file:

function dynaminc_modal() {
   ob_start();
   header('Content-Type: text/html; charset=utf-8');
   include plugin_dir_url( __FILE__ ) . '/path/to/dynamic_modal.php';
   $string = ob_get_clean();
   exit($string);
}

And add the ajax action*:

add_action('wp_ajax_dynamic_modal', 'dynamic_modal');

We will be able to call the file like so (truncated from above):

    ed.windowManager.open({
        file : url + ajaxurl + "?action=dynaminc_modal", // ajaxurl is a global var in WP admin
        width : ... ,
        height : ...,
        inline : 1
        }, ....

wp_ajax in this context will load a streamlined version of wp core.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.