Using post ID in custom tinyMCE button

You would need to place a globally namespaced javascript variable in your php code where you enqueue the script to be loaded for the editor pages.

So, this code will enqueue a script function to be added to the “edit post/page” screens:

add_action('admin_head','my_add_styles_admin');
function my_add_styles_admin() {

    global $current_screen;
    $type = $current_screen->post_type;

    if (is_admin() && $type == 'post' || $type == 'page') {
        ?>
        <script type="text/javascript">
        var post_id = '<?php global $post; echo $post->ID; ?>';
        </script>
        <?php
    }
}

Now, in your editor_plugin.js file for your tinymce button; you can access this post ID by simply calling the post_id javascript variable.

Leave a Comment