0

This question already has an answer here:

I'm actually working on a small shortcodes plugin.

On plugin main file I register TinyMCE button and link core js about button:

add_filter("mce_external_plugins", "shortcodes_tinymce_plugin");
add_filter("mce_buttons", "register_shortcodes_button");

function register_shortcodes_button($buttons) {
    array_push($buttons, "separator", "shortcodes");
    return $buttons;
}

function shortcodes_tinymce_plugin($plugin_array) {
    $plugin_array['shortcodes'] = PLUGINDIR_URI . 'tiny_mce_script.js';
    return $plugin_array;
}

Which contains this code:

(function() {  

 tinymce.create('tinymce.plugins.shortcodes', {

    init : function(ed, url) {              

        ed.addButton('shortcodes', {  
            title : 'Utilizer shortcodes',  
            image : url.replace('/js','')+'/img/shortcodes.png',  
            cmd : 'shortcodes',
            pluginurl : ajaxurl                
        });

        ed.addCommand('shortcodes', function() {
            ed.windowManager.open({
                file : url.replace('/js','')+'../../tinymce_shortcode.php',
                width : 510 + parseInt(ed.getLang('button.delta_width', 0)),
                height : 415 + parseInt(ed.getLang('button.delta_height', 0)),
                inline : 1
            }, {
                plugin_url : url
            });
        });

    }

 });

 tinymce.PluginManager.add('shortcodes', tinymce.plugins.shortcodes);
 }
)();

When you click the button modal page loads this file tinymce_shortcode.php which contains this content on top of document:

<?php 

require_once("../../../../wp-load.php"); 

if ( !current_user_can('publish_posts') ) exit();

wp_register_script( 'shortcodes',   PLUGINDIR_URI . "shortcodes.js", false, "1.0", true );   
wp_register_script( 'tiny-mce-popup',   PLUGINDIR_URI . "tiny_mce_popup.js", false, "1.0", true );   

wp_enqueue_script( 
    array( 
        'iris', 
        'json2', 
        'jquery', 
        'jquery-ui-core', 
        'jquery-ui-sortable',
        'jquery-ui-tabs',
        'jquery-ui-slider',
        'jquery-ui-datepicker',
        'jquery-ui-autocomplete',
        'tiny-mce-popup',
        'shortcodes',
        'media-upload', 
        'thickbox',
    )  
); ?>

Then on bottom right before body gets closed I execute this code:

<?php do_action('admin_print_scripts'); ?>
<?php do_action('admin_print_footer_scripts'); ?>

All of javascript libraries are loaded like jquery, jquery-ui, media-upload, thickbox, jso2 etc. Only iris the color picker ones do not get loaded. Any help is really appreciated.

marked as duplicate by kaiser, fuxia Jan 8 '14 at 17:46

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Please show us your actual code, not only what error output your get. – kaiser Jan 8 '14 at 16:05
  • require_once("../../../../wp-load.php"); <-- there has got to be a better way! – GhostToast Jan 8 '14 at 16:11
  • @kaiser my main problem here is to load iris color picker, and the code to load iris is up there. – mentorgashi Jan 8 '14 at 16:14
  • @GhostToast do you have any better idea ? – mentorgashi Jan 8 '14 at 16:15
  • Please show what you currently got... – kaiser Jan 8 '14 at 16:16
0

Your problem probably is the following line in your JavaScript file:

file : url.replace('/js','')+'../../tinymce_shortcode.php'

First, this way you're bringing it out of context. Just load it up front when you init the plugin (and when you're on a post.php or post-new.php request - see current_screen() return values to narrow that down to the needed requests where you want to load your plugin).

The TinyMCE windowManager isn't meant to load PHP files. It can load HTML files to outsource the needed MarkUp/the view. Nothing more. If you need to generate MarkUp server side, simply build it inside a function or method and push the result to your JS file by localizing this values (more about it in the next paragraph). Then use either .confirm() or .open() depending on your task.

Next, you're replace-ing URis. That makes it highly dependent on the actual file location. Remember that the plugin directory can be moved. And that additional plugin directories can be added next to the original one. Simply don't do that. If you need a relative URL in a JavaScript file, simply use wp_localize_script() and plugins_url( __FILE__, "your-file.php" ) and plugin_dir_path( __FILE__ )."your-file.php". Not that you'd need that in this case.

Last, never ever go and load stuff outside the WP context if you aren't loading it outside of WP at all. In short: If you need to load wp-load.php - and therefore the whole WP core - you're doing something pretty wrong.

  • Based on your answer I will have to change the concept to stick with wp core and not load it externally. However the solution that I found to this problem was this and worked pretty well. wordpress.stackexchange.com/questions/82718/… – mentorgashi Jan 8 '14 at 17:03
  • For the next time, please use the search before asking the question :P – kaiser Jan 8 '14 at 17:05
  • I did but couldn't figure out what I was doing, thanks a lot for your time and effort on this one, really appreciating it. – mentorgashi Jan 8 '14 at 17:06

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