I’m developing a WordPress plugin and I need to get the current Post ID
in the Write Post / Write Page editing screen (outside the loop).
I also need to do it before the “admin_print_scripts” hook, since I would like to pass some data to a javascript file.
I can’t use:
$id = $_GET['post'];
because the url doesn’t include this variable when you are adding a new post or page.
So far I’ve tried these options but none of them worked:
A) This returns an ID of 0
function myplugin_setup() {
global $wp_query;
$id = $wp_query->get_queried_object_id();
var_dump($id);
}
add_action('admin_init', 'myplugin_setup' );
B) This returns an ID of null
function myplugin_setup() {
global $wp_query;
$id = $wp_query->post->ID;
var_dump($id);
}
add_action('admin_init', 'myplugin_setup' );
C) This also returns an ID of null
function myplugin_setup() {
global $post;
$id = $post->ID;
var_dump($id);
}
add_action('admin_init', 'myplugin_setup' );
Make sure you call the global $post after the WordPress query. If you add an action to init or admin_init, the query isn’t ready so there’s nothing you can get out of the global $post variable.
My advice would be to check the action reference from this page: http://codex.wordpress.org/Plugin_API/Action_Reference and choose one that works for you.
For example, I did this:
And I was able to get the page ID in WP admin
Use:
at the beginning of your function. You should then have access to $post->ID to get the id of the current post. This will work for new and existing posts.
For anyone still wondering the correct hook to call, or one of the many in the wordpress admin lifecycle, is: admin_head
as follows:
Problem is, you are using admin_init hook. If you look into Action Reference – http://codex.wordpress.org/Plugin_API/Action_Reference – you will see, that this hook is actually called BEFORE quering posts, that’s why those variables you use are not yet filled.
You can use some later action (with some is_admin() check), or you may use admin init hook to add action to some later hook, so again it will be used only on admin.
The ‘admin_init’ action is triggered before any other hook when a user accesses the admin area. It is triggered before the new post gets an id.
To get the new post id you can use ‘save_post’, which is an action triggered whenever a post or page is created or updated (http://codex.wordpress.org/Plugin_API/Action_Reference/save_post).
First you can include your scripts using ‘admin_enqueue_scripts’, then use ‘save_post’ to get the new post id. The ‘admin_print_scripts’ is triggered after ‘save_post’ and you can use wp_localize_script (https://codex.wordpress.org/Function_Reference/wp_localize_script), or other method to pass the new post id to your javascript.
I needed something similar, but it was used in a class.
Now, in your javascript you can use:
I think it will help you get the id in admin panel.
If it is a new post/page, I guess the id does not exists yet, because the post has not been published/added to DB. If you are trying to edit a post/page, I think you may use
$id = $_GET['post']
;this may work :