I’m building a simple plugin and need to be able to access current post id when user clicks custom tinyMCE button (inside its onclick function). How should I get current post ID to do that.
Just for this example, code from this tutorial:
can be used, and after clicking on the button, current post id could be logged into console (console.log) or alerted to screen.
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
So, this code will enqueue a script function to be added to the “edit post/page” screens:
Now, in your editor_plugin.js file for your tinymce button; you can access this post ID by simply calling the
I found a simpler solution, maybe someone can use it:
var post_id = jQuery('#post_ID').val();