2

I am not sure if it is possible or not.

Can i use wp_localize_script with mce_external_plugins filter?

I want to send a variable to the tinymce plugin script. For Example:

add_filter( "mce_external_plugins", array( &$this, 'add_test_plugin' ) );

public function add_test_plugin( $plugin_array ){

    global $pagenow;

    if( is_admin() && $pagenow == "post.php" || $pagenow == "post-new.php" ){
        $plugin_array['mytest'] = plugin_dir_url(__FILE__) . '/js/testing.js';
        return $plugin_array;
    }

}

I have to send a variable to testing.js? How do i achieve this?

Update:

this is the link that help me to resolve my problem Using post ID in custom tinyMCE button

0

If I understand correctly; you just need to make a Javascript variable available to testing.js.

I think it would be just as useful to send the variable with jQuery, as that would be loaded prior to TinyMCE anyway:

add_action('wp_enqueue_scripts', 'YOUR_NAME_scripts'); //back end

function YOUR_NAME_scripts( $hook_suffix ) {

        global $blog_id;
        $params = array(
            'site_url' => site_url(),
            'admin_ajax_url' => site_url() . '/wp-admin/admin-ajax.php',
            'mytest' => $whatever_variable_value


         );

            wp_localize_script( 'jquery', 'YOUR_JAVASCRIPT_VARIABLE_HOLDER', $params );

}

Then you can access the mytest variable in testing.js by simply using YOUR_JAVASCRIPT_VARIABLE_HOLDER.mytest anywhere in the script.

  • sorry but wp_localize_script is not working with 'jquery' and it is not passed my variable to the script. – jogesh_pi Apr 11 '14 at 18:31
  • make sure when registering your script, you tell it to load jquery as a dependency. example: wp_register_script('your-testing', plugin_dir_url(__FILE__) . '/js/testing.js', array('jquery') ); – Douglas.Sesar Apr 11 '14 at 18:50
  • as you see in my codes $plugin_array['mytest'] = plugin_dir_url(__FILE__) . '/js/testing.js'; i am not registering my script. This is one way to load js for tinymce button. That's why wp_localize_script not working. – jogesh_pi Apr 12 '14 at 3:39
  • You should register your script the Wordpress way. It makes life easier for everyone when we have coding standards. – Douglas.Sesar Apr 12 '14 at 18:16
  • 1
    i did another solution with the help of this thread wordpress.stackexchange.com/questions/121093/…, and this helped to achieve what i want :). And really thankful to you to continuing with me :) – jogesh_pi Apr 12 '14 at 18:25
0

The WordPress documentation on the mce_external_plugins filter hook proposes to use two filter hooks to add your variable to testing.js: admin_head-post.php and admin_head-post-new.php https://codex.wordpress.org/Plugin_API/Filter_Reference/mce_external_plugins

This is the sample:

foreach ( array('post.php','post-new.php') as $hook ) {
     add_action( "admin_head-$hook", 'my_admin_head' );
}

/**
 * Localize Script
 */
function my_admin_head() {
    $plugin_url = plugins_url( '/', __FILE__ );
    ?>
<!-- TinyMCE Shortcode Plugin -->
<script type='text/javascript'>
var my_plugin = {
    'url': '<?php echo $plugin_url; ?>',
};
</script>
<!-- TinyMCE Shortcode Plugin -->
    <?php
}

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.