Skip to content
Advertisement

get the current page id inside wordpress plugin page

I need to get the current page id in WordPress plugin page outside the loop. And the code I wrote for getting current page id is in my plugin page. I tried many codes, but doesn’t work

JavaScript

But it doesn’t work for me .

JavaScript

This is also not working.

When I try

JavaScript

Then a error message appears

Fatal error: Call to a member function get_queried_object() on a non-object in H:xampphtdocswordpresswp-contentpluginswpkwpk.php on line 876

When I print:

JavaScript

then result is:

JavaScript

I don’t know how to solve this, how to get the current page id.If you know how to solve this, then I need your support. Thanks in advance.

Advertisement

Answer

get_the_ID(); or $post->ID; returns the current page or post id in WordPress.

But you need to ensure that your post is saved in wordpress post table. Other wise you can’t get the id , simply because of it is not an entry in wordpress database.

If it is a static page and it’s not an entry in wordpress post then, get_the_ID() didn’t return anything.

For example : get_the_ID() didn’t go to work in post archive pages , administration pages in wordpress backend etc.

So as per this question you are trying to get the id of the page that is a backend plugin setting page or any archive page .

UPDATE

Method to get the current post id in wordpress

(1) global $post; $post->ID();

(2) global $wp_query; $post_id = $wp_query->get_queried_object_id();

(3) global $wp_query; $post_id = $wp_query->post->ID;

(4) get_the_ID();

[ It is recommended that this tag must be within The Loop. ]

see this

JavaScript

ie get_the_ID() return the id of current $post .

(5) get_query_var('page_id')

[ it will not going to work if we use pretty permalink ]
https://codex.wordpress.org/Function_Reference/get_query_var

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement